阅读背景:

Codeforces Educational Codeforces Round 42 (Rated for Div. 2) D Merge Equals 自定义优先队列(C++加速输入输出流)

来源:互联网 

点击打开链接

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=150000+5;
LL a[maxn];
struct node{
LL x;
int y;
node(LL x,int y):x(x),y(y) {}
friend bool operator<(node N1,node N2)
{
    if(N1.x!=N2.x) return N1.x>N2.x;
    else if(N1.y!=N2.y) return N1.y>N2.y;
}
};
int main()
{
    std::ios::sync_with_stdio(false);               //加速C++输入输出流
    cin.tie(0);
    int n;
    cin>>n;
    priority_queue<node>pq;
    while(!pq.empty()) pq.pop();
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        pq.push(node(a[i],i));
    }
    while(!pq.empty())
    {
        node p1=pq.top(); pq.pop();
        node p2=node(-1,-1);
        bool flag=false;
        while(p1.x!=p2.x&&!pq.empty()) {
            if(flag) {
                p1=p2;
            }
            else flag=true;
            p2=pq.top(); pq.pop();
        }
        if(p1.x==p2.x) {
            a[min(p1.y,p2.y)]=-1;
            a[max(p1.y,p2.y)]=2*p1.x;
            pq.push(node(2*p1.x,max(p1.y,p2.y)));
        }
    }
    int cnt=0;
    for(int i=0;i<n;i++)
        if(a[i]!=-1) cnt++;
    cout<<cnt<<endl;
    for(int i=0;i<n-1;i++)
    {
        if(a[i]==-1) continue;
        cout<<a[i]<<" ";
    }
    cout<<a[n-1]<<endl;
    return 0;
}#include<bits/stdc++.h>
using namespac



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: