输入
1 2 2 1 1
输出
1 1 1 2 2
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
class node {
public:
int cnt;
int index;
};
bool cmp(const pair<int, node> &a, const pair<int, node> &b)
{
if (a.second.cnt != b.second.cnt)
return a.second.cnt > b.second.cnt;
else
return a.second.index < b.second.index;
}
/*
输入
1 2 2 1 1
输出
1 1 1 2 2
*/
int main()
{
int n, c;
cin >> n >> c;
unordered_map<int, node> mp;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
if (mp.find(x) == mp.end())
{
mp.insert({ x, {1, i} });
}
else
{
mp[x].cnt++;
}
}
vector<pair<int, node>> nums(mp.begin(), mp.end()); // 把 map 整体拷贝,元素按 pair 处理
sort(nums.begin(), nums.end(), cmp);
for (int i = 0; i < (int)nums.size(); i++)
{
for (int j = 0; j < (int)nums[i].second.cnt; j++)
{
cout << nums[i].first << ' ';
}
}
return 0;
}