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
 //题目大意:就是顾客买东西之前,根据顾客的购买历史进行商品推荐
//所以购买第一件商品3是没有推荐的,因为没有购买历史记录
//然后根据前面购买的历史次数,进行次数从多到少进行推荐,最多推荐三个,相同次数的推荐标号最小的的//
//使用set来得到按要求的排序规格,vector来辅助查询set中是否已经存在元素
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int n, k;
struct Node
{
int value, cnt;
bool operator < (const Node& a)const//set的排序规则
{
return (cnt != a.cnt) ? cnt > a.cnt:value < a.value;
}
};
int main()
{
int n, k, num;
scanf("%d %d", &n, &k);
set<Node>s;
vector<int>v(n + , );
for (int i = ; i < n; ++i)
{
cin >> num;
if (i != )//第一件商品没有推荐
{
cout << num << ":";
int i = ;
for (auto ptr = s.begin(); ptr != s.end() && i <= k; ++ptr, i++)
cout << " " << ptr->value;
cout << endl;
}
auto ptr = s.find(Node{ num,v[num] });
if (ptr != s.end())//记住set不能修改内容,只能擦除后重新写入
s.erase(ptr);
v[num]++;
s.insert(Node{ num,v[num] });//使用了初始化列表原则
}
return ;
}

PAT甲级——A1129 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

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

  3. PAT A1129 Recommendation System (25 分)——set,结构体重载小于号

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

  4. A1129. Recommendation System

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

  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甲级】1070 Mooncake (25 分)(贪心水中水)

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

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

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

  8. PAT甲级 1122. Hamiltonian Cycle (25)

    1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

  9. PAT甲级 1121. Damn Single (25)

    1121. Damn Single (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue "Dam ...

随机推荐

  1. context:component-scan报错

    文件头补上 xmlns:context=”http://www.springframework.org/schema/context” xsi:schemaLocation=”http://www.s ...

  2. IIS查找报错信息

    1.打开IIS,选择日志 2.选择 日志文件和ETW事件   ---->应用 3.在发布网站的根目录下找一个logs文件(如果没有则创建) 4.浏览网站,如果报错,logs文件夹下面就会生成一个 ...

  3. leetcood学习笔记-39-组合总和

    题目描述: 方法一: class Solution: def combinationSum(self, candidates, target): """ :type ca ...

  4. 拦截器一Interceptor

    import org.springframework.web.servlet.HandlerInterceptor; 前言 拦截器,在AOP(Aspect-Oriented Programming)中 ...

  5. spring boot 项目启动无法访问,排查

    查看docker日志,后台应用正常启动,定时任务正常执行,但是前端无法访问到后端接口,点击提示系统错误,解压出项目二级域名,访问域名,报错:Kong Error,说明Kong路由转发没有绑定项目端口, ...

  6. Perl 运算符

    Perl 运算符 运算符是一种告诉编译器执行特定的数学或逻辑操作的符号,如: 3+2=5. Perl 语言内置了丰富的运算符,我们来看下常用的几种: 算术运算符 比较运算符 逻辑运算符 赋值运算符 位 ...

  7. jQuery 事件 click() 方法,dblclick() 方法

    click() 方法 当点击元素时,会发生 click 事件. 当鼠标指针停留在元素上方,然后按下并松开鼠标左键时,就会发生一次 click. click() 方法触发 click 事件,或规定当发生 ...

  8. 剑指offer——31序列化二叉树

    题目描述 请实现两个函数,分别用来序列化和反序列化二叉树   二叉树的序列化是指:把一棵二叉树按照某种遍历方式的结果以某种格式保存为字符串,从而使得内存中建立起来的二叉树可以持久保存.序列化可以基于先 ...

  9. 通过Matlab SDK 获取tushare数据

    概要说明 版本要求:Matlab需要2016b及以上版本 接口说明:可以用help pro_api和help pro_bar查看 demo程序:请参考tushare_pro_test.m文件 程序包下 ...

  10. json对象数组的创建、遍历、添加、删除、修改、js的splice()用法

    本文链接:https://blog.csdn.net/houfengfei668/article/details/79843625 )第二种方式:手动构造json对象数组 )for )用splice方 ...