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

时间:2022-07-26 12:21:08

模拟题,运用强大的stl。

 #include <iostream>
#include <map>
#include <algorithm>
#include <set> #define N 150005
using namespace std;
typedef long long Int;
const int maxn = 1e9 + ;
map<long long, set<int> > mapp;
long long a[N]; int main()
{
int n;
cin >> n;
int all = n;
for (int i = ; i <= n; i++)
{
cin >> a[i];
mapp[a[i]].insert(i);
}
for (map<long long, set<int> >::iterator i = mapp.begin(); i != mapp.end(); i++)
{
while (i->second.size() > )
{
set<int>::iterator op1 = i->second.begin(), op2;
op2 = op1; op2++;
all--;
int t1 = *op1, t2 = *op2;
a[t1] = -;
a[t2] *= ;
mapp[a[t2]].insert(t2);
i->second.erase(op1);
i->second.erase(op2);
}
}
cout << all << endl;
for (int i = ; i <= n; i++)
{
if (a[i] != -)
cout << a[i] << " ";
} //system("pause");
return ;
}