You are given an array of positive integers. While there are at least two equal elements, we will perform the following operation. We choose the smallest value x
Determine how the array will look after described operations are performed.
For example, consider the given array looks like [3,4,1,2,2,1,1]. It will be changed in the following way: [3,4,1,2,2,1,1] → [3,4,2,2,2,1] → [3,4,4,2,1] → [3,8,2,1].If the given array is look like [1,1,3,1,1]it will be changed in the following way: [1,1,3,1,1] → [2,3,1,1] → [2,3,2] → [3,4].
The first line contains a single integer n(2≤n≤150000) — the number of elements in the array.
The second line contains a sequence from n elements a1,a2,…,an (1≤ai≤109) — the elements of the array.
In the first line print an integer k— the number of elements in the array after all the performed operations. In the second line print kintegers — the elements of the array after all the performed operations.
#include<bits/stdc++.h>
using namespace std;
long long a[150005];
map<long long,int>g;//保存下标
map<long long,bool>apper;//判断是否已经出现过
int n;
int main()
{
scanf("%d",&n);
for(int i = 1; i <= n; i++)
{
scanf("%lld",&a[i]);
}
for(int i = 1; i <= n; i++)
{
if(apper[a[i]])//已经出现
{
a[g[a[i]]] = -1;
apper[a[i]] = false;
a[i] = 2*a[i];
if(!apper[a[i]])//没有出现,更新下标
g[a[i]] = i;
i--;
}
else
{
g[a[i]] = i;
apper[a[i]] = true;
}
}
int cnt = 0;
for(int i = 1; i <= n; i++)
{
if(a[i] > 0)
cnt++;
}
cout<<cnt<<endl;
for(int i = 1; i <= n; i++)
{
if(a[i] > 0)
printf("%lld ",a[i]);
}
return 0;
}