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. vue 基础知识 随笔

    window.localStorage.gettItem("someItem"||[])//如果localStorage中的someItem不存在就返回一个空数组 window.l ...

  2. MySQL Archive存储引擎

    200 ? "200px" : this.width)!important;} --> 介绍 从archive单词的解释我们大概可以明白这个存储引擎的用途,这个存储引擎基本上 ...

  3. 洛谷P3371 【模板】单源最短路径(弱化版)(SPFA解法)

    题目背景 本题测试数据为随机数据,在考试中可能会出现构造数据让SPFA不通过,如有需要请移步 P4779. 题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输 ...

  4. [Qt Creator 快速入门] 第3章 窗口部件

    从这一章开始正式接触Qt的窗口部件.在第2章曾看到 Qt Creator 提供的默认基类只有 QMainWindow.QWidget 和 QDialog 这3种.QMainWindow 是带有菜单栏和 ...

  5. 【SpringMVC框架】非注解的处理器映射器和适配器

    参考来源:     http://blog.csdn.net/acmman/article/details/46968939 处理器映射器就是根据URL来找Handler,处理器适配器就是按照它要求的 ...

  6. iPhone4到iPhone6的设计、制造工艺历程浅析

    这里就阐述一下我对这几个手机在设计和制造工艺上的一些看法. 这 5 个型号概括起来就三个外观: iPhone4 的三明治夹心设计. iPhone5 的三段式铝合金一体成型. iPhone6 的全金属一 ...

  7. Redis作者:深度剖析Redis持久化

    Redis是一种面向“key-value”类型数据的分布式NoSQL数据库系统,具有高性能.持久存储.适应高并发应用场景等优势.它虽然起步较晚,但发展却十分迅速. 近日,Redis的作者在博客中写到, ...

  8. css样式获取及兼容性(原生js)

    类选择器兼容性 getbyclass()类选择器,在IE8及以下均不可用. // 类选择器的兼容性 function getbyclass(parentName,Name){ var parentNa ...

  9. Elasticsearch--地理搜索

    目录 地理位置索引 空间搜索映射定义 示例 基于距离的排序 边界框过滤 距离的限制 任意地理形状搜索 点 包络线 多边形 多个多边形 把形状保存到索引中 地理位置索引 空间搜索映射定义 elastic ...

  10. Android Error:Unable to find method 'com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List;'.

    问题:Error:Unable to find method 'com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List ...