Home PAT 1129 Recommendation System
Post
Cancel

PAT 1129 Recommendation System

Link

set 原来还可以这么用 ( )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <set>
using namespace std;
int n,k,a,items[50010];
struct node{
    int id,num;
    // node(int _id,int _num): id(_id), num(_num){}
    bool operator<(const node &x)const{
        return num!=x.num?num>x.num:id<x.id;
    }
};
set<node>st;
int main() {
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;++i){
        scanf("%d",&a);
        if(i>1){
            printf("%d:",a);
            int cnt=0;
            for(auto it=st.begin();cnt<k&&it!=st.end();++it){
                printf(" %d",it->id);
                cnt++;
            }
            printf("\n");
        }
        auto it=st.find(node{a,items[a]});
        if(it!=st.end()) st.erase(it);
        items[a]++;
        st.insert(node{a,items[a]});
    }
    return 0;
}
This post is licensed under CC BY 4.0 by the author.