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

本文通过一个具体的算法题目示例,展示了如何利用C++标准模板库(STL)中的map和set等容器来简化复杂的数据操作过程。该示例涉及了对一系列数值进行特定规则的操作,最终输出经过处理后的数据集合。

模拟题,运用强大的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 }

 

转载于:https://www.cnblogs.com/ouyang_wsgwz/p/8809916.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值