PAT 1129 Recommendation System
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的更多相关文章
- PAT 1129 Recommendation System[比较]
1129 Recommendation System(25 分) Recommendation system predicts the preference that a user would giv ...
- PAT甲级 1129. Recommendation System (25)
1129. Recommendation System (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 1129 Recommendation System
1129 Recommendation System (25 分) Recommendation system predicts the preference that a user would gi ...
- PAT 甲级 1129 Recommendation System
https://pintia.cn/problem-sets/994805342720868352/problems/994805348471259136 Recommendation system ...
- PAT A1129 Recommendation System (25 分)——set,结构体重载小于号
Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...
- 1129 Recommendation System (25 分)
Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...
- 1129. Recommendation System (25)
Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...
- PAT甲题题解-1129. Recommendation System (25)-排序
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789819.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT1129:Recommendation System
1129. Recommendation System (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
随机推荐
- Python基础第六天
一.内容 二.练习 练习1 题目:文件的增删改查 图示: 代码: import os def add(data): content = data[1] # 文件内容 file_name = data[ ...
- MySQL5.7修改字符集
本人安装的mysql版本是5.7.20,安装好mysql后就要对字符集进行修改了,于是照着网上的大部分教程说的去安装目录找一个my-default.ini文件,然后重命名为my.ini,再对其进修改字 ...
- js moment.js日期操作类 datetime,日期操作,dayjs
http://momentjs.com/ JS时间处理插件MomentJS https://juejin.im/post/5a2bdc55f265da432b4abf5e Day.js 2kB超轻量时 ...
- DHTML_____如何编写事件处理程序
<html> <head> <meta charset="utf-8"> <title>如何编写事件处理程序</title&g ...
- JavaScript相关技术学习
百度前端技术学院 http://ife.baidu.com/task/all 框架 JQuery 简单Web论坛 https://github.com/huanshen/web-bbs http:/ ...
- 数据采集框架Gobblin简介
问题导读: Gobblin的架构设计是怎样的? Gobblin拥有哪些组建,如何实现可扩展? Gobblin采集执行流程的过程? 前面我们介绍Gobblin是用来整合各种数据源的通用型ETL框架,在某 ...
- Java学习笔记-eclipse配置
一.配置Java环境变量 JAVA_HOME:D:\Program Files\Java\jdk1.7.0_76 CLASSPATH:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HO ...
- how to do a mass update in Laravel5 ( 在Laravel 5里面怎么做大量数据更新 )
Today, I had spent 3 hours to fix one problem, The old program has a bug, originally, when a user pr ...
- Code Kata:大整数四则运算—除法 javascript实现
除法不可用手工算法来计算,其基本思想是反复做减法,看从被除数里面最多能减去多少个除数,商就是多少. 除法函数: 如果前者绝对值小于后者直接返回零 做减法时,不需要一个一个减,可以以除数*10^n为基数 ...
- 联想 S5 Pro GT(L78091)免解锁BL 免rec 保数据 ROOT Magisk Xposed 救砖 ZUI5.0.047
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...