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 分)
72 0
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
101 0
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
115 0
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
127 0
|
存储
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
119 0
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
98 0
PAT (Advanced Level) Practice - 1016 Phone Bills(25 分)
PAT (Advanced Level) Practice - 1016 Phone Bills(25 分)
101 0
PAT (Advanced Level) Practice - 1018 Public Bike Management(30 分)
PAT (Advanced Level) Practice - 1018 Public Bike Management(30 分)
181 0
PAT (Advanced Level) Practice - 1103 Integer Factorization(30 分)
PAT (Advanced Level) Practice - 1103 Integer Factorization(30 分)
87 0
PAT (Advanced Level) Practice - 1107 Social Clusters(30 分)
PAT (Advanced Level) Practice - 1107 Social Clusters(30 分)
123 0