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 (<= 50000), 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>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int n,k,d,c;
int rec[];
int times[];///记录出现次数
struct item
{
int x,c;
item (int x,int c)
{
this -> x = x;
this -> c = c;
}
friend bool operator < (item a,item b)
{
if(a.c == b.c)return a.x > b.x;
return a.c < b.c;
}
};
int main()
{
cin>>n>>k;
priority_queue<item> q;
for(int i = ;i < n;i ++)
{
cin>>d;
times[d] ++;
c = ;
while(!q.empty() && c < k)
{
rec[c ++] = q.top().x;
q.pop();
}
while(!q.empty())q.pop();
if(c)
{
cout<<d<<':';
for(int i = ;i < c;i ++)
{
cout<<' '<<rec[i];
if(rec[i] != d)q.push(item(rec[i],times[rec[i]]));
}
cout<<endl;
}
q.push(item(d,times[d]));
}
}

1129. Recommendation System (25)的更多相关文章

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

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

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

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

  3. 1129 Recommendation System

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

  4. PAT 1129 Recommendation System[比较]

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

  5. 1129 Recommendation System (25 分)

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

  6. PAT 甲级 1129 Recommendation System

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

  7. PAT 1129 Recommendation System

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

  8. PAT1129:Recommendation System

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

  9. PAT_A1129#Recommendation System

    Source: PAT A1129 Recommendation System (25 分) Description: Recommendation system predicts the prefe ...

随机推荐

  1. C# DataTable 增加行与列

    亲测有用的方法 DataTable AllInfos = new DataTable();//生成一个表格 DataColumn typeColumn = new DataColumn();//建一个 ...

  2. 【C++进阶】 to_string,stringstream

    to_string函数主要进行以下一些参数转换为string stringstream,位于<sstream>库中 https://blog.csdn.net/jllongbell/art ...

  3. 5、Shiro之jdbcRealm认证授权

    登录认证: 注意,下面我是以连接orcal数据库为例的依赖,如果各位同仁使用的是骑她数据库,可以换成对应数据库的依赖(数据源不用换) Pom.xml增加依赖: <!--引入连接orcal的jar ...

  4. 激活Win Server 2008 R2 Datacenter

    直接手撸KMS命令 管理员打开cmd输入: slmgr /skms kms.03k.org 然后用这个Key: 74YFP-3QFB3-KQT8W-PMXWJ-7M648

  5. 《Using Python to Access Web Data》Week4 Programs that Surf the Web 课堂笔记

    Coursera课程<Using Python to Access Web Data> 密歇根大学 Week4 Programs that Surf the Web 12.3 Unicod ...

  6. 动画演示 Delphi 2007 IDE 功能[2] - 定义变量

    https://my.oschina.net/hermer/blog/319152 动画剧本: 第一个变量: 输入: var; 然后执行 Ctrl+J ... 回车 第二个变量: 执行 Ctrl+J; ...

  7. Python中的sorted函数

    今天在做一个中文文本分类的项目,遇到了一个sorted函数,发现并不会用... 记录一下: sorted(list, key, reverse) list是给定的列表: key是排序过程调用的函数,也 ...

  8. Linux系统常用命令之top

    top - 06:58:37 up 7 days, 23:36, 2 users, load average: 0.00, 0.01, 0.05Tasks: 716 total, 1 running, ...

  9. Vue 实现手动刷新组件

    Vue 实现手动刷新组件:https://www.jianshu.com/p/742142dc95f3

  10. java基础笔记(2)

    java中成员变量是有默认初始值的,而局部变量是没有的: 构造方法名和类名相同,没有返回值,即结构如下:public 构造方法名(): 实例化类的本质就是调用了类的构造方法: 如果自定义了构造方法,就 ...