1129 Recommendation System (25 分)

参考柳神代码,考察(set)的自定义排序。

const int N=50010;
struct Node
{
    int item;
    int times;
    bool operator<(const Node &W) const
    {
        if(times != W.times) return times > W.times;
        return item < W.item;
    }
};
set<Node> S;
int cnt[N];
int n,k;

int main()
{
    cin>>n>>k;

    for(int i=0;i<n;i++)
    {
        int x;
        scanf("%d",&x);
        if(i)
        {
            printf("%d:",x);
            int tot=0;
            for(auto t:S)
            {
                cout<<' '<<t.item;
                if(++tot == k) break;
            }
            puts("");
        }
        auto it=S.find({x,cnt[x]});
        if(it != S.end()) S.erase(it);
        cnt[x]++;
        S.insert({x,cnt[x]});
    }
    //system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/fxh0707/p/14490085.html