962D - Merge Equals - CodeForces Solution


data structures implementation *1600

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define int long long 
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define pre(i,x,y) for(int i=x;i>=y;i--)
using namespace std;
const int INF=0x3f3f3f3f,mod=1e9+7;

int qpow(int a,int n){
	int num=1;
	while(n){
    	if(n&1)num=num*a;
    	a=a*a;
    	n>>=1;
	}
	return num;
}
int qmul(int a,int b,int mod){
    int num=0;
	while(b){
		if(b&1)num=(num+a)%mod;
        a=a*2%mod;
        b>>=1;
    }
	return num;
}
const int N=1e6+10;

int a[N];
multiset<pair<int,int>>s;
vector<int>ans;
void solve(){
	int n;
	cin>>n;
	rep(i,1,n){
		int x;
		cin>>x;
		s.insert({x,i});
	}
	while(s.size()){
		auto i=s.begin();
		auto j=next(i);
		//cout<<i->first<<" "<<j->first<<endl;
		if(i->first==j->first){
			int sum=i->first*2;
			s.insert({sum,j->second});
			s.erase(i);
			s.erase(j);
		}else{
			a[i->second]=i->first;
			s.erase(i);
		}
	}
	rep(i,1,n){
		if(a[i])ans.push_back(a[i]);
	}
	cout<<ans.size()<<endl;
	for(auto i:ans){
		cout<<i<<" ";
	}
}

void debug(){
	
}
signed main(){ 
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	debug();
	int T=1;
	//scanf("%lld",&T);
	while(T--)solve();
	
    return 0;
}
/*

*/
 	  			  	 				    		  		 					


Comments

Submit
0 Comments
More Questions

1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push
706B - Interesting drink
1265A - Beautiful String
214A - System of Equations
287A - IQ Test
1108A - Two distinct points
1064A - Make a triangle
1245C - Constanze's Machine
1005A - Tanya and Stairways
1663F - In Every Generation
1108B - Divisors of Two Integers
1175A - From Hero to Zero
1141A - Game 23
1401B - Ternary Sequence
598A - Tricky Sum
519A - A and B and Chess
725B - Food on the Plane