Recommendation system predicts the preference that a user would give to an item. Now you are asked to program a very simple recommendation system that rates the user's preference by the number of times that an item has been accessed by this user.

Input Specification:

Each input file contains one test case. For each test case, the first line contains two positive integers: N (≤ 50,000), the total number of queries, and K (≤ 10), the maximum number of recommendations the system must show to the user. Then given in the second line are the indices of items that the user is accessing -- for the sake of simplicity, all the items are indexed from 1 to N. All the numbers in a line are separated by a space.

Output Specification:

For each case, process the queries one by one. Output the recommendations for each query in a line in the format:

query: rec[1] rec[2] ... rec[K]

where query is the item that the user is accessing, and rec[i] (i=1, ... K) is the i-th item that the system recommends to the user. The first K items that have been accessed most frequently are supposed to be recommended in non-increasing order of their frequencies. If there is a tie, the items will be ordered by their indices in increasing order.

Note: there is no output for the first item since it is impossible to give any recommendation at the time. It is guaranteed to have the output for at least one query.

Sample Input:

12 3

3 5 7 5 5 3 2 1 8 3 8 12

Sample Output:

5: 3

7: 3 5

5: 3 5 7

5: 5 3 7

3: 5 3 7

2: 5 3 7

1: 5 3 2

8: 5 3 1

3: 5 3 1

8: 3 5 1

12: 3 5 8

#include<iostream> //利用set,并自定义比较符号来自动排序
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
int val, cnt;
bool operator<(const node& t) const{
return (cnt==t.cnt?val<t.val:cnt>t.cnt);
}
};
vector<int> v(50005, 0);
int main(){
int n, k;
cin>>n>>k;
set<node> s;
for(int i=0; i<n; i++){
int num;
scanf("%d",&num);
if(i){
printf("%d:",num);
int tempcnt=0;
for(auto it=s.begin(); it!=s.end()&&tempcnt<k; it++){
printf(" %d",it->val);
tempcnt++;
}
printf("\n");
}
auto it=s.find(node{num, v[num]});
if(it!=s.end())
s.erase(it);
v[num]++;
s.insert(node{num, v[num]});
}
}

PAT 1129 Recommendation System的更多相关文章

  1. PAT 1129 Recommendation System[比较]

    1129 Recommendation System(25 分) Recommendation system predicts the preference that a user would giv ...

  2. PAT甲级 1129. Recommendation System (25)

    1129. Recommendation System (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  3. 1129 Recommendation System

    1129 Recommendation System (25 分) Recommendation system predicts the preference that a user would gi ...

  4. PAT 甲级 1129 Recommendation System

    https://pintia.cn/problem-sets/994805342720868352/problems/994805348471259136 Recommendation system ...

  5. PAT A1129 Recommendation System (25 分)——set,结构体重载小于号

    Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...

  6. 1129 Recommendation System (25 分)

    Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...

  7. 1129. Recommendation System (25)

    Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...

  8. PAT甲题题解-1129. Recommendation System (25)-排序

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789819.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  9. PAT1129:Recommendation System

    1129. Recommendation System (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

随机推荐

  1. Mysql中分页查询两个方法比较

    mysql中分页查询有两种方式, 一种是使用COUNT(*)的方式,具体代码如下 1 2 3 SELECT COUNT(*) FROM foo WHERE b = 1;   SELECT a FROM ...

  2. (转载)基于React与Vue后,移动开源项目Weex如何定义未来

    https://www.ctolib.com/topics-106347.html vuejs+ts+webpack2框架的项目实践 https://mp.weixin.qq.com/s/p2Uc9I ...

  3. 略微讲讲最近的 webpack 该如何加快编译

    首先假设 基础的环境是有 creat-react-app 所创建的 即所有基础的loader,插件的 cache 都已经缓存了 在这种情况下想加速,真是很难 不过,有一个插件是可以观察 各个模块所花的 ...

  4. 一张图带你了解-常见面试之JUC包详解

    面试时经常问到JUC包下的类及特性,现在用一张图总结下

  5. 生成自签名ca 证书 使nginx 支持https

    创建服务器私钥,命令会让你输入一个口令:$ openssl genrsa -des3 -out server.key 1024创建签名请求的证书(CSR):$ openssl req -new -ke ...

  6. WIN2012的桌面和开始菜单跑到什么地方去了

    传统开始菜单取消了,你把鼠标指针移动到左下角的边缘,会有一个小浮窗,就是开始,再点开始就能进到一个全屏的层里面,跟手机界面,那个玩意就是新一代的开始菜单,程序菜单,你在开始菜单上,鼠标右键点一下,下面 ...

  7. Redis基础---消息通信模式

    Redis发送订阅通信模式 Redis发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. Redis 发布订阅(pub/sub)实现了消息系统,发送者( ...

  8. A8ERP配送管理系统

  9. arduino 字符解析

    Arduino String.h库函数详解   此库中包含1 charAT()2 compareTo()3 concat()4 endsWith()5 equals()6 equalslgnoreCa ...

  10. Android Studio 1.5启动出现“SDK Manager: failed to install”问题的解决

    问题描述 Android Studio 1.5是当前最新Android手机应用开发平台,下载bundle版安装后,启动Studio后出现“SDK Manager: failed to install” ...