1857C - Assembly via Minimums - CodeForces Solution


greedy sortings

Please click on ads to support us..

C++ Code:

////////////////////////////hume to cp n luta dsa mai kaha dum tha////////////////////////
#include<bits/stdc++.h>
using namespace std;
#define int                    long long
#define pb                     push_back
#define pob                    pop_back
#define lb                     lower_bound
#define ub                     upper_bound
#define sz(x)                  (int)(x).size()
#define len(x)				   (int)(x).length()
#define all(x)				   x.begin(), x.end()
#define maxe(x)				   max_element(x.begin(), x.end());
#define mine(x)				   min_element(x.begin(), x.end());
#define rev(x) 				   reverse(all(x))


#define ndl                   cout << "\n";
#define YES      			   cout<<"YES"<<endl;
#define NO       			   cout<<"NO"<<endl;
#define Yes      			   cout<<"Yes"<<endl;
#define No       			   cout<<"No"<<endl;
#define minus    			   cout<<"-1"<<endl;
#define jiro  			 	   cout<<0<<endl;

#define vi   				   vector<int>
#define vvi  	     		   vector<vector<int>> 
#define mii  	     		   map<int,int>
#define pii  	     		   pair<int, int>
#define vpi  	     		   vector<pii>
#define umii 	     		   unordered_map<int, int>
#define uset 	     		   unordered_set


#define imin            	  LLONG_MIN
#define imax                  LLONG_MAX
#define fi                    first
#define se                    second
#define set_bits              __builtin_popcountll
#define setpr(i)              cout << setprecision(i);
#define mems(t,x)             memset(t , x , sizeof(t)) ;  
#define print(x)              cout<<x<<endl;
#define rep(i,k,n)            for (int i = k; i < n; i++)
#define repb(i,n, k)           for (int i = n; i >= k; i--)


// ll mod_add(ll a, ll b, ll m){a = a %m}


#ifndef ONLINE_JUDGE
#define dbg(x...) cerr << #x<<" " ; _print(x) ;cerr<< endl;
#else
#define dbg(x...);
#endif

//templates debugging 
void _print(double a){cerr<<a<<endl;}
void _print(int a ){cerr<<a<<endl;}
void _print(char a){cerr<<a<<endl;}
// void _print(long long a ){cerr<<a<<endl;}
void _print(long double a ){cerr<<a<<endl;}
void _print(string a){cerr<<a<<endl;}
void _print(bool a){cerr<<a<<endl;}

// input template
template <class T> void input(vector<T> &v , int n){for(int i =0;i<n;i++){T x;cin>>x;v.pb(x);}}
template <class T> void input(vector<vector<T>> &v , int n , int m){for(int i =0;i<n;i++){vector<T> temp;for(int j =0;j<m;j++){T x;cin>>x;temp.pb(x);}v.pb(temp);}}

void init1(){
 #ifndef ONLINE_JUDGE 
	
    freopen("inputf.in", "r", stdin);
    freopen("outputf.in", "w", stdout);
#endif
}
void init2(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("degub.in", "w", stderr);
    #endif
}

template <class T , class C > void _print(pair<T,C> p){cerr<<p.ft<<" "<<p.ss<<endl;}
template <class T> void _print(vector<T> a){cerr<<"[";for(auto &i:a){cerr<<i<<" ";}cerr<<"]";cerr<<endl;}
template <class T> void _print(set<T> a){cerr<<"[";for(auto &i:a){cerr<<i<<" ";}cerr<<"]";cerr<<endl;}
template <class T> void _print(unordered_set<T> a){cerr<<"[";for(auto &i:a){cerr<<i<<" ";}cerr<<"]";cerr<<endl;}
template <class T , class C> void _print(map<T,C> a){for(auto &i:a){cerr<<"["<<i.first<<"->"<<i.second<<"] ";}cerr<<endl;}
template <class T , class C> void _print(unordered_map<T,C> a){for(auto &i:a){cerr<<"["<<i.first<<"->"<<i.second<<"] ";}cerr<<endl;}
template <class T> void _print(vector<vector<T>> a){for(auto &i:a){cerr<<"[";for(auto &j:i){cerr<<j<<" ";}cerr<<"]";}}
template <class T > void _print(multiset<T> a){for(auto &i:a){cerr<<i<<" ";}cerr<<endl;}
///////////////////////////HUME TO CP Nai luta dsa mai kha dum tha/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


void solve(){

	int n;cin >> n; 
	int ttl =( n*(n-1)) / 2;
	vi v;
	input(v, ttl);

	sort(all(v));


	vi ans(n, imin);
	int num = 0;

	int  i = 0;	
	while(num<n){

		int j = i;
		int ele= v[i];
		int cnt = n - num - 1;
		while(cnt--){
			j++;
		}
		ans[num] = ele;
		i = j;
		num++;
	}

		ans[n-1] = 1e9;

	rep(i, 0, n) cout<<ans[i]<<" ";
	cout<<endl;

}





signed main()
{
	init1();
	init2();
	
	int t ;cin >> t;
	
	while(t--){
		solve();

	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

1632B - Roof Construction
388A - Fox and Box Accumulation
451A - Game With Sticks
768A - Oath of the Night's Watch
156C - Cipher
545D - Queue
459B - Pashmak and Flowers
1538A - Stone Game
1454C - Sequence Transformation
165B - Burning Midnight Oil
17A - Noldbach problem
1350A - Orac and Factors
1373A - Donut Shops
26A - Almost Prime
1656E - Equal Tree Sums
1656B - Subtract Operation
1656A - Good Pairs
1367A - Short Substrings
87A - Trains
664A - Complicated GCD
1635D - Infinite Set
1462A - Favorite Sequence
1445B - Elimination
1656C - Make Equal With Mod
567A - Lineland Mail
1553A - Digits Sum
1359B - New Theatre Square
766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase