Educational Codeforces Round 42 D. Merge Equals (set + pll)
Posted ckxkexing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Educational Codeforces Round 42 D. Merge Equals (set + pll)相关的知识,希望对你有一定的参考价值。
题意:
给定一个数列,对于靠近左端的两个相同大小的值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 << " "; #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 ‘ ‘ #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 ", cnt); for(int i=1; i<=n; i++){ if(!vis[i])printf("%lld " , a[i]); } printf(" "); }
以上是关于Educational Codeforces Round 42 D. Merge Equals (set + pll)的主要内容,如果未能解决你的问题,请参考以下文章
Educational Codeforces Round 7 A
Educational Codeforces Round 7
Educational Codeforces Round 90
Educational Codeforces Round 33