https://blog.csdn.net/tigerisland45/article/details/54772817
C++ 代码
/* CCF201503-2 数字排序 */
#include <iostream>
#include <map>
#include <queue>
using namespace std;
struct node {
int key, count;
bool operator < (const node& n) const {
if(count == n.count)
return key > n.key;
else
return count < n.count;
}
};
int main()
{
priority_queue<node> q;
map<int, int> m;
int n, v;
// 输入数据,构建Map
cin >> n;
for(int i=0; i<n; i++) {
cin >> v;
m[v]++;
}
// 构建优先队列进行排序
node keyval;
for(map<int,int>::iterator it=m.begin(); it!=m.end(); it++) {
keyval.key = it->first;
keyval.count = it->second;
q.push(keyval);
}
// 输出结果
while(!q.empty()) {
keyval = q.top();
q.pop();
cout << keyval.key << " " << keyval.count << endl;
}
return 0;
}
m保存没个数出现多少次,node是优先队列单位,push进去的时候被定义了比较方法