PAT (Advanced Level) Practice - 1129 Recommendation System(25 分)

简介: PAT (Advanced Level) Practice - 1129 Recommendation System(25 分)

题目链接:点击打开链接

题目大意:略。

解题思路:略。

AC 代码

#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a)
#define ssclr(ss) ss.clear(), ss.str("")
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;
const int maxn=5e5+10;
struct node
{
    int val,cnt;
    node(int val,int cnt):val(val),cnt(cnt){}
    bool operator<(const node &nd)const
    {
        //return false 数据插入失败,而不是(插入成功,只是位置不一样)。
        return cnt!=nd.cnt ? cnt>nd.cnt : val<nd.val ;
    }
};
set<node> st;
int mp[maxn];
int main()
{
    int n,k,a,l;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a);
        if(i)
        {
            printf("%d:",a);
            l=0;
            for(auto it=st.begin(); it!=st.end()&&l<k; it++)
            {
                l++;
                printf(" %d",(*it).val);
            }
            puts("");
        }
        auto it=st.find(node(a,mp[a]));
        if(it!=st.end()) st.erase(it);
        mp[a]++;
        st.insert(node(a,mp[a]));
    }
    return 0;
}
目录
相关文章
|
移动开发 C语言
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
94 0
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
119 0
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
130 0
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
140 0
PAT (Advanced Level) Practice - 1103 Integer Factorization(30 分)
PAT (Advanced Level) Practice - 1103 Integer Factorization(30 分)
99 0
PAT (Advanced Level) Practice - 1118 Birds in Forest(25 分)
PAT (Advanced Level) Practice - 1118 Birds in Forest(25 分)
109 0
|
存储
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
138 0
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
117 0
PAT (Advanced Level) Practice - 1107 Social Clusters(30 分)
PAT (Advanced Level) Practice - 1107 Social Clusters(30 分)
144 0
PAT (Advanced Level) Practice - 1013 Battle Over Cities(25 分)
PAT (Advanced Level) Practice - 1013 Battle Over Cities(25 分)
119 0