天天看點

CCF201503-2 數字排序

可能有更簡便的做法,自己優先隊列不太熟練,就試着用這個了。。。

#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int a[1100];

struct node{
    int num, cnt;
    bool operator<(const node &a)const{
        if(cnt != a.cnt)
            return cnt < a.cnt;
        else
            return num > a.num;
    }
}Node[1100];
int main()
{
    priority_queue<node> q;
    int n;
    cin>>n;
    int num, maxx = 0;
    for(int i = 1 ; i <= n ; ++i)
    {
        cin>>num;
        a[num]++;
        maxx = max(maxx, num);
        
    }
    for(int i = 1 ; i <= maxx ; ++i)
    {
        if(a[i] > 0)
        {
        node no;
        no.num = i;
        no.cnt = a[i];
            q.push(no);
        }
        
    }
    while(!q.empty())
    {
        node t = q.top();
        q.pop();
        cout<<t.num<<" "<<t.cnt<<endl;
    }
    return 0;
    
}


           

繼續閱讀