div2D
sol:
每次找到两个最小的数,如果相同则塞回去一个,不同则存下小的作为答案
复习一波优先队列
#include <bits/stdc++.h> using namespace std; #define int long long typedef int ll; inline ll read() { ll s=0; bool f=0; char ch=' '; while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();} while(isdigit(ch)) {s=(s<<3)+(s<<1)+(ch^48); ch=getchar();} return (f)?(-s):(s); } #define R(x) x=read() inline void write(ll x) { if(x<0) {putchar('-'); x=-x;} if(x<10) {putchar(x+'0'); return;} write(x/10); putchar((x%10)+'0'); } #define W(x) write(x),putchar(' ') #define Wl(x) write(x),putchar(' ') const int N=200005; int n,a[N]; struct node { int v,id; bool operator<(const node &tmp) const { return (tmp.v==v)?tmp.id<id:tmp.v<v; } }lwj[N]; priority_queue<node>que,wwx; inline bool cmp(node pp,node qq) { return pp.id<qq.id; } signed main() { // freopen("codeforces.in","r",stdin); int i,cnt=0; R(n); for(i=1;i<=n;i++) R(a[i]); if(n==1) { Wl(1); Wl(a[1]); return 0; } for(i=1;i<=n;i++) que.push((node){a[i],i}); // for(i=1;i<=n;i++) // { // node tmp=que.top(); // cout<<tmp.id<<" "<<tmp.v<<endl; // que.pop(); // } node wn,wq; while(que.size()>1) { wn=que.top(); que.pop(); wq=que.top(); que.pop(); // cout<<wn.v<<" "<<wn.id<<" "<<wq.v<<" "<<wq.id<<endl; if(wn.v^wq.v) { wwx.push(wn); que.push(wq); } else { que.push((node){wn.v*2,wq.id}); } } wwx.push(que.top()); Wl((int)wwx.size()); while(wwx.size()>0) { lwj[++cnt]=wwx.top(); wwx.pop(); } sort(lwj+1,lwj+cnt+1,cmp); for(i=1;i<=cnt;i++) W(lwj[i].v); return 0; }