1129 Recommendation System (25 分)

 1 #pragma warning(disable:4996)
 2 #define _CRT_SECURE_NO_WARNINGS
 3 
 4 #include <iostream>
 5 #include <map>
 6 #include <set>
 7 using namespace std;
 8 struct node
 9 {
10     int ind, num;
11     node(int x) :ind(x){}
12     node(int x, int y):ind(x), num(y){}
13     bool operator < (const node& a)const
14     {
15         return num != a.num ? num > a.num : ind < a.ind;
16     }
17 };
18 //int book[50001];
19 int main()
20 {
21     int n, k;
22     cin >> n >> k;
23     set<node> s;
24     for (int i = 0; i < n; ++i)
25     {
26         int a;
27         scanf("%d", &a);
28         if (i != 0)
29         {
30             printf("%d:", a);
31             int cnt = 0;
32             for (auto it = s.begin(); it != s.end() && cnt < k; ++cnt, ++it)
33             {
34                 printf(" %d", it->ind);
35             }
36             cout << endl;
37         }
38         auto it = s.find({ node(a), });//找到下标为a的元素
39         if (it != s.end())
40         {
41             s.erase(it);
42         }
43         int t = it->num;
44         s.insert({ node(a, t+1)});
45     }
46     return 0;
47 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值