D. Merge Equals(from Educational Codeforces Round 42 (Rated for Div. 2))

Posted jaydenouyang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D. Merge Equals(from Educational Codeforces Round 42 (Rated for Div. 2))相关的知识,希望对你有一定的参考价值。

模拟题,运用强大的stl。

 1 #include <iostream>
 2 #include <map>
 3 #include <algorithm>
 4 #include <set>
 5 
 6 #define N 150005
 7 using namespace std;
 8 typedef long long Int;
 9 const int maxn = 1e9 + 7;
10 map<long long, set<int> > mapp;
11 long long a[N];
12 
13 int main()
14 {
15     int n;
16     cin >> n;
17     int all = n;
18     for (int i = 1; i <= n; i++)
19     {
20         cin >> a[i];
21         mapp[a[i]].insert(i);
22     }
23     for (map<long long, set<int> >::iterator i = mapp.begin(); i != mapp.end(); i++)
24     {
25         while (i->second.size() > 1)
26         {
27             set<int>::iterator op1 = i->second.begin(), op2;
28             op2 = op1; op2++;
29             all--;
30             int t1 = *op1, t2 = *op2;
31             a[t1] = -1;
32             a[t2] *= 2;
33             mapp[a[t2]].insert(t2);
34             i->second.erase(op1);
35             i->second.erase(op2);
36         }
37     }
38     cout << all << endl;
39     for (int i = 1; i <= n; i++)
40     {
41         if (a[i] != -1)
42             cout << a[i] << " ";
43     }
44 
45     //system("pause");
46     return 0;
47 }

 

以上是关于D. Merge Equals(from Educational Codeforces Round 42 (Rated for Div. 2))的主要内容,如果未能解决你的问题,请参考以下文章

Educational Codeforces Round 30 D. Merge Sort

codeforces 962D Merge Equals

CF962D Merge Equals

Codeforces 962D - Merge Equals

Educational Codeforces Round 42 (Rated for Div. 2) D - Merge Equals

[RxJS] Reactive Programming - New requests from refresh clicks -- merge()