版权声明: https://blog.csdn.net/yyy_3y/article/details/79978426
题意:给你n个数,然后如果同一个数出现过两次或者多次的话,从左边开始往右边,把第一个删掉,第二个变成2倍的值。
思路:强大的STL!map和set搞一搞就行。
#include<bits/stdc++.h>
#define debug(a) cout << #a << " " << a << endl
#define LL long long
#define ull unsigned long long
#define PI acos(-1.0)
#define eps 1e-6
const int N=2e5+7;
using namespace std;
LL a[N],ans[N];
map<LL,set<int>>mp;
int main ()
{
//yyy_3y
//freopen("1.in","r",stdin);
int n;scanf("%d",&n);
for(LL i=1;i<=n;i++){
scanf("%lld",&a[i]);
mp[a[i]].insert(i);
}
LL ret=0;
for(auto &it:mp){
while(it.second.size()>=2){
it.second.erase(it.second.begin());
mp[it.first*2].insert(*it.second.begin());
it.second.erase(it.second.begin());
}
if(it.second.size()==1){
ans[*it.second.begin()]=it.first;
ret++;
}
}
printf("%lld\n",ret);
for(auto &i:ans){
if(i) printf("%lld ",i);
}
return 0;
}
#include<bits/stdc++.h>
#define debug(a) cout << #a << " " << a << endl
#define LL long long
#define ull unsigned long long
#define PI acos(-1.0)
#define eps 1e-6
const int N=2e5+7;
using namespace std;
map<LL,int>mp;
LL a[N];
int main ()
{
//yyy_3y
//freopen("1.in","r",stdin);
int n;scanf("%d",&n);
int ret=0;
for(LL i=1;i<=n;i++){
scanf("%lld",&a[i]);
while(mp[a[i]]){
a[mp[a[i]]]=0;
mp[a[i]]=0;
a[i]*=2;
ret++;
}
mp[a[i]]=i;
}
printf("%d\n",n-ret);
for(auto &i:a) if(i) printf("%lld ",i);
return 0;
}