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

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

代码:

#include <bits/stdc++.h>
using namespace std; int N, K;
int mp[50010]; struct Node {
int val, cnt;
bool operator < (const Node &a) const {
return (cnt != a.cnt) ? cnt > a.cnt : val < a.val;
}
}; int main() {
scanf("%d%d", &N, &K);
set<Node> s;
for(int i = 1; i <= N; i ++) {
int num;
scanf("%d", &num);
if(i != 1) {
printf("%d:", num);
int outnum = 0;
for(set<Node>::iterator it = s.begin(); outnum < K && it != s.end(); it ++) {
printf(" %d", it -> val);
outnum ++;
}
printf("\n");
}
set<Node>::iterator it = s.find(Node{num, mp[num]});
if(it != s.end()) s.erase(it);
mp[num] ++;
s.insert(Node{num, mp[num]});
}
return 0;
}

  终于熬过期末可以每天安心写代码 开心~~

这道题之前自己写的超时 后来看了一哈题解 用到 set 重载 "<" 还是没怎么看明白 哭唧唧

PAT 甲级 1129 Recommendation System的更多相关文章

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

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

  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. PAT 1129 Recommendation System[比较]

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

  4. 1129 Recommendation System

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

  5. PAT 1129 Recommendation System

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

  6. 1129 Recommendation System (25 分)

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

  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 (25)-排序

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

  9. PAT甲级题解(慢慢刷中)

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

随机推荐

  1. Go Web 使用工具

    前端工具: sublime text3 下载:https://www.sublimetext.com/3 中文版设置:https://jingyan.baidu.com/article/9c69d48 ...

  2. Go语言中多字节字符的处理

    1 概述 Go语言的字符串是使用 UTF-8 编码的.UTF-8 是 Unicode 的实现方式之一.本文内容包括:UTF-8 和 Unicode 的关系,Go语言提供的 unicode 包和 uni ...

  3. 学习笔记二:异步FIFO

      , ) //用格雷码的局限性:循环计数深度必须是2的n次幂,否则就失去了每次只变化一位的特性 (wclk,wrstn,wdata,wfull,winc,rclk,rrstn,rdata,rempt ...

  4. 20155232 2016-2017-3 《Java程序设计》第4周学习总结

    20155232 2016-2017-3 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 继承与多态 所谓继承就是避免多个类间重复定义共同行为. 1.重复在程序设计上就是不好 ...

  5. 20155233 《Java程序设计》第十二周课堂练习总结

    20155233 <Java程序设计> 第十二周课堂练习总结 测试题目 1.修改教材P98 Score2.java, 让执行结果数组填充是自己的学号:提交在IDEA或命令行中运行结查截图, ...

  6. 20155310 2016-2017-2 《Java程序设计》第十周学习总结

    20155310 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程 •网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就 ...

  7. 20155327 2016-2017-2《Java程序设计》课程总结

    20155327 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:http://www.cnblogs.com/l97----/p/6160983.htm ...

  8. 20145234黄斐《java程序设计》第三周

    教材学习内容总结 类与对象 定义:对象,与过程相对. Java中变量有2种类型,一个是基本类型,另一个则是类类型.基本类型在之前学过,本次学习类类型.使用Java撰写程序几乎都是在使用对象,要产生对象 ...

  9. BZOJ4034_树上操作_KEY

    题目传送门 这道题可以树链剖分+线段树. 其他操作模板,第二个操作只需要将x~x+size[x]-1区间加值即可. code: #include <cstdio> #include < ...

  10. Oracle的物理存储与逻辑存储关系对应

    逻辑结构: TableSapce 由 多个 Segment组成 Sgement 由多个 Extent 组成 Extent 由 多个数据块组成 物理结构: 一个Tablespace 可以包括多个数据文件 ...