Educational Codeforces Round 42 D. Merge Equals (set + pll)

CF962D

题意:

   给定一个数列,对于靠近左端的两个相同大小的值x可以合并成一个点。把x 乘以2 放在第二个点的位置,问最后的数列大小和每个位子的值。

思路:

  利用set 配上 pair 就行了,感觉很巧妙,每次取出前两个pll  t1,t2。 如果 t1.first != t2.first ,把t2直接重新放入set中,否则,把t2.first * 2并更新t2.second 位子,把t2放入到set中。(这么说好像优先队列也可以)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <cstdlib>
#include <iterator>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <cctype>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int ,pii> p3;
//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B; A <= C; ++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const double PI=acos(-1.0);

template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}

/*-----------------------show time----------------------*/
            const int maxn = 2e5+9;
            ll a[maxn],vis[maxn];
            set<pll>s;
int main(){
            int n;
            scanf("%d", &n);
            for(int i=1; i<=n; i++){
                scanf("%lld", &a[i]);
                s.insert(pll(a[i],i));
            }

            while(s.size() > 1){
                pll t = *s.begin();
                s.erase(s.begin());
                pll x = *s.begin();
                s.erase(s.begin());
                // cout<<t.se << " "<<x.se<<endl;
                if(x.fi == t.fi){
                    vis[t.se] = 1;
                    x.fi = x.fi + x.fi;
                    a[x.se] = x.fi;
                }
                // debug(x.fi);
                s.insert(x);
            }
            int cnt = 0;
            for(int i=1; i<=n; i++)if(vis[i]==0)cnt++;
            printf("%d\n", cnt);
            for(int i=1; i<=n; i++){
                if(!vis[i])printf("%lld " , a[i]);
            }
            printf("\n");
}
CF962D

 

posted @ 2018-08-30 21:53  ckxkexing  阅读(161)  评论(0编辑  收藏  举报