题目链接:https://pintia.cn/problemsets/994805342720868352/problems/994805348471259136
题意:根据用户每次点击的东西的编号,输出他在点当前编号之前应该给这个用户推荐的商品的编号~只推荐k个~也就是输出用户曾经点击过的商品编号的最多的前k个~如果恰好两个商品有相同的点击次数,就输出编号较小的那个
#include<bits/stdc++.h>
using namespace std;
struct node
{
int x;
int num;
// friend bool operator <(const node &a,const node &b)
// {
// return a.num!=b.num?a.num>b.num:a.x<b.x;
// }
node(int a,int b)
{
x = a;
num = b;
}
bool operator <(const node &a) const
{
return a.num!=num?num>a.num:x<a.x;
}
};
set<struct node> s;
int tt[110000];
int main()
{
int n,k;
cin>>n>>k;
int i;
for(i=0; i<n; i++)
{
int x;
scanf("%d",&x);
if(i)
{
printf("%d:",x);
int ans = 0;
set<node>::iterator it = s.begin();
for(it; it != s.end() && ans < k; it++)
{
printf(" %d",it->x);
ans++;
}
putchar('\n');
}
set<struct node> :: iterator it;
it = s.find(node(x,tt[x]));
if(it!=s.end())
s.erase(it);
tt[x]++;
s.insert(node(x,tt[x]));
}
return 0;
}