显示原始代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
struct node {
ll x;
int pos;
bool operator<(const node a) const {
if (x != a.x)
return x > a.x;
return pos > a.pos;
}
};
struct cmp {
bool operator()(const node a, const node b) const { return a.pos > b.pos; }
};
priority_queue<node> Q;
priority_queue<node, vector<node>, cmp> ans;
map<ll, int> mp;
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
node st = { x, i };
Q.push(st);
mp[x]++;
}
while (!Q.empty()) {
start:
node st = Q.top();
Q.pop();
bool flag = 0;
if (mp[st.x] >= 2) {
node ne = Q.top();
Q.pop();
ne.x *= 2;
mp[st.x] -= 2;
mp[ne.x]++;
Q.push(ne);
flag = 1;
}
if (mp[st.x] >= 2)
goto start;
if (mp[st.x] == 1 && !flag)
ans.push(st);
if (!mp[st.x])
mp.erase(st.x);
}
int size = ans.size();
printf("%d\n", size);
for (int i = 1; i <= size; i++) {
node st = ans.top();
ans.pop();
printf("%lld%c", st.x, " \n"[i == size]);
}
return 0;
}