140C - New Year Snowmen - CodeForces Solution


binary search data structures greedy *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll f[120000][4];
ll a[123456];
int main(){
    ll n; cin>>n;
	for(ll i=1;i<=n;i++){ cin>>a[i]; }
	sort(a+1,a+n+1);
	ll l=1,r=n/3;
	ll x=0,j,v;
	while(l<=r){
	    ll mid=(l+r)/2;
        bool flag=false;
        for(int  i=1;i<=mid;i++){
            f[i][0]=a[i];
        }
        j=1, v=0;
        for(ll i=mid+1;i<=n;i++){
            if(a[i]>f[j][v]){
                f[j][v+1]=a[i];
                j++;
                if(j>mid) j=1 ,v=v+1;
                if(v==2) flag= true;
            }
        }
        if(flag) {
            l=mid+1;
            x=mid;
        }
        else{r=mid-1;}

	}

	for(int  i=1;i<=x;i++){
        f[i][0]=a[i];
    }
    j=1, v=0;
    for(ll i=x+1;i<=n;i++){
        if(a[i]>f[j][v]){
            f[j][v+1]=a[i];
            j++;
            if(j>x){j=1 ,v=v+1;}
        }
    }
    cout<<x<<endl;
    for(ll i=1;i<=x;i++){
        cout<<f[i][2]<<' '<<f[i][1]<<' '<<f[i][0]<<endl;
    }
}


Comments

Submit
0 Comments
More Questions

1511C - Yet Another Card Deck
1698A - XOR Mixup
1702E - Split Into Two Sets
1703B - ICPC Balloons
1702F - Equate Multisets
1700A - Optimal Path
665C - Simple Strings
1708A - Difference Operations
1703E - Mirror Grid
1042A - Benches
1676B - Equal Candies
1705B - Mark the Dust Sweeper
1711A - Perfect Permutation
1701B - Permutation
1692A - Marathon
1066A - Vova and Train
169B - Replacing Digits
171D - Broken checker
380C - Sereja and Brackets
1281B - Azamon Web Services
1702A - Round Down the Price
1681C - Double Sort
12A - Super Agent
1709A - Three Doors
1680C - Binary String
1684B - Z mod X = C
1003A - Polycarp's Pockets
1691B - Shoe Shuffling
1706A - Another String Minimization Problem
1695B - Circle Game