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. 7、Shiro加密和加盐

    这里我们以md5加密方法举例,首先我们写一个main方法测试我们的密码经过md5加密之后的得到什么样的字符串: /** * 书写方法测试Md5Hash将密码“houru”加密之后的密文 * 但是仅仅加 ...

  2. Jmeter接口测试系列之判断测试结果

    在使用jmeter进行接口测试时,我们需要根据测试用例判断其测试结果是否一致,判断其执行是否成功. 在jmeter中也可以使用响应断言去判断,但是有时我们需要根据不同的用例,其判断条件不同,所以这里我 ...

  3. js监听当前页面再次加载

    document.addEventListener("visibilitychange", function () { if (!document.hidden) { //处于当前 ...

  4. C—LINQ小结

    LINQ代表语言集成查询(Language-Integrated Query),它包括用于从数据源检索信息的一组功能.数据检索是许多程序的重要组成功能. 简介:System.Linq; var num ...

  5. 使用python脚本部署mariadb主从架构

    环境准备 一个脚本自动部署master服务 另一个部署slave服务 关闭主从节点的防火墙 以及事先设置好root远程登陆的权限. grant all on *.* to root@'%'  iden ...

  6. oracle ogg--ogg搭建过程中遇到的错误及处理

    1 PRVF-0002 : Could not retrieve local nodename---# Begin Stacktrace #---------------------------ID: ...

  7. 以非root身份安装Python的Module或者Package以及pip安装指定路径

    因为要远程访问公司的服务器,没有sudo的权限,所以在安装python的一些包的时候就不能安去默认路径了(比如以/usr/local/lib/为prefix的路径). 一般来讲用easy_instal ...

  8. idea中怎么去查看maven项目的依赖包是否有冲突

    1:快捷键:

  9. ipad已停用 连接itunes怎么办

    问题描述: ipad 开机密码多次输入出错后,提示 ipad已停用 连接itunes 解决方法: 参考: https://jingyan.baidu.com/article/fb48e8bee9ef4 ...

  10. 第四周预习作业and第五周作业

    第四周预习作业 统计一行文本的单词个数 本题目要求编写程序统计一行字符中单词的个数.所谓"单词"是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以是多个. 输入格式: 输入 ...