1129. Recommendation System (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
————————————————————————————————
题目的意思是给出n个数,对于每个数,输出他前面的出现k个数,k个数以出现次数多优先,次数一样从小到大
思路:开个set记录每个数和出现次数,每次输出前k个
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define LL long long
const int inf=0x3f3f3f3f; struct node
{
int x,t;
bool friend operator<(node a,node b)
{
if(a.t!=b.t)
return a.t>b.t;
return a.x<b.x;
}
}; int main()
{
int n,k,x;
int vis[100005];
node f,ans[100];
memset(vis,0,sizeof vis);
set<node>s;
scanf("%d%d",&n,&k);
scanf("%d",&x);
f.x=x;
f.t=1;
s.insert(f);
vis[x]=1;
for(int i=1; i<n; i++)
{
scanf("%d",&x);
printf("%d:",x);
int cnt=0;
set<node>::iterator it=s.begin();
for(; cnt<k&&it!=s.end(); it++,cnt++)
{
printf(" %d",(*it).x);
}
printf("\n");
f.x=x;
f.t=vis[x];
it=s.find(f);
if(it!=s.end())
s.erase(it);
vis[x]++;
f.x=x;
f.t=vis[x];
s.insert(f);
}
return 0;
}

  

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

  1. PAT 甲级 1129 Recommendation System

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

  2. PAT甲级——A1129 Recommendation System【25】

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

  3. 1129. Recommendation System (25)

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

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

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

  5. PAT 1129 Recommendation System[比较]

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

  6. 1129 Recommendation System

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

  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

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

  9. 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

    题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...

随机推荐

  1. c# Parallel.For 并行编程 执行顺序测试

    因为有个for 实际执行结果尽管是按照for里边的顺序执行,但处理器让哪个分线程先执行,谁先处理完就不一定了. 对于要求结果需要先后顺序的,比如对text内容的操作, 用并行 Parallel.For ...

  2. windbg 加载dump监测蓝屏原因时出现重复的星星框提示解决办法

    加载dump文件出现了重复上边的信息 总结: 要reload一下环境,说明符号库未加载出现的错误,!sym noisy 显示详细信息 ,reload重载符号库,未找到的,从服务器进行下载, 注意,第一 ...

  3. list集合与HashMap的使用

    List<Map<String, Object>> arrays= new ArrayList<Map<String, Object>>(); Hash ...

  4. 1N - 计算球体积

    根据输入的半径值,计算球的体积. Input 输入数据有多组,每组占一行,每行包括一个实数,表示球的半径. Output 输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数. Sam ...

  5. C++11并发编程实战 免费书籍

    C++11 博客http://www.cnblogs.com/haippy/p/3284540.html 网上推荐的C++多线程基本都是C++ Concurrency in Action 英文版的,中 ...

  6. libusb开发

    转:https://www.cnblogs.com/ele-eye/p/3261970.html

  7. robotframework 运行集合

    Robot Framework 运行测试通过 pybot 命令,检查 _C:\Python36\Scripts_ 目录下是否有 pybot.bat 文件,正确安装 Robot Framework 一定 ...

  8. keepalived配虚拟ip(vip)的作用

    keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议. 虚拟路由冗余协议,可以认为是实现路由器高可用的 ...

  9. servlet 高级知识之Filter

    Filter叫做拦截器, 对目标资源拦截,拦截HTTP请求和HTTP响应,本质是对url进行拦截. 与serlvet不同的是, Filter的初始化是随着服务器启动而启动. 在Filter接口中定义了 ...

  10. Eclipse的SVN插件使用

    1       eclipse的SVN插件使用 1.1     svn插件安装 下载Subversion的eclipse插件 http://subclipse.tigris.org/servlets/ ...