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 }

 

posted @ 2018-04-12 19:19  ouyang_wsgwz  阅读(178)  评论(0编辑  收藏  举报