1. 程式人生 > >Educational Codeforces Round 42 D. Merge Equals (set + pll)

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

equal ati urn int merge 大根堆 pro begin tdi

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

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