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. AndroidStudio设置SVN忽略文件

    方法一: 在SVN中进行设置: 在空白处右键单击,选择TortoiseSVN -> Settings ->General:在General界面找到Global ignore pattern ...

  2. class 用法 函数变量的作用域

    函数变量的作用域 1. 函数体内声明的变量 2. 参数中的变量 没有赋值的 function fn(a){} 赋值的,值不是变量 function fn(a=45){} 赋的值为变量 function ...

  3. Nor Flash芯片特性分析

    Nor Flash是Intel在1988年推出的非易失闪存芯片,可随机读取,擦写时间长,可以擦写1~100W次,支持XIP(eXecute In Place). 本文以JS28F512M29EWH为例 ...

  4. java kryo序列化与反序列化

    https://blog.csdn.net/lan12334321234/article/details/84907492 问题: https://blog.csdn.net/baidu_384041 ...

  5. Jquery Ajax调用asmx出错问题

    1.//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释.      [System.Web.Script.Services.ScriptService] 这个 ...

  6. C#中winform下利用ArcEngine调用ArcGIS Server发布的服务 AE 10

    开发环境:vs2010 + AE 10 测试 public Form1() { ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Engi ...

  7. Mysql-5.7 x64安装

    首先在官网下载Mysql:https://dev.mysql.com/downloads/mysql/ 选择ZIP Archive下载. 下载安装之后配置环境变量: 编辑现有环境变量Path: PS: ...

  8. Oracle 启用登录终端超时锁定功能

    远程连接oracle 会出现超时连接断开的问题,所以需要修改oracle配置. 修改超时时间10分钟 ALTER PROFILE DEFAULT LIMIT IDLE_TIME 10; 查询修改后的超 ...

  9. vs2010发布网站时有些文件没有发布出去的解决办法。

    项目中包含了一些ttf字体文件做为图标使用,可是发布时发现生成的目录中没有这个文件,这种情况这么设置一下就可以解决: 1,在文件上点击右键,选择“属性”. 2,在弹出的属性窗口中,更改“生成操作”为“ ...

  10. dotnet core排序异常,本地测试和linux上结果不一致

    根据汉字排序,本地测试结构正常,发到docker之后,发现汉字升序降序和本地相反,检查代码后,没找到任何可能出现问题的点. 然后去翻文档:字符串比较操作 看到了这一句,会区分区域性 然后猜测应该是do ...