1129 Recommendation System PAT 甲级真题(输出格式,set使用)

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-----------------------------------set<>可以在Node里面重载运算符,注意格式输出。注意重载运算符的方向,
	#include<iostream>
	#include<set>

	using namespace std;
	int n, k;
	struct Node {
		int id, cnt;
		bool operator<(const Node &a)const {
			return cnt != a.cnt ? cnt > a.cnt:id < a.id;
		}
	};
	int pr[50001];
	set<Node>ans;
	int main()
	{
		cin >> n >> k;
		for (int i = 0; i < n; i++) {
			int u; cin >> u;
			int tmp = 0;
			if (i != 0) {
				printf("%d:", u);
				for (auto j = ans.begin(); tmp < k&&j != ans.end(); j++, tmp++) {
					printf(" %d",j->id);
				}
				printf("\n");
			}
			auto it = ans.find({u,pr[u]});
			if (it != ans.end()) ans.erase(it);
				pr[u]++;
			ans.insert({ u,pr[u]});
		}
		getchar(); getchar();
		return 0;
	}

  

原文地址:https://www.cnblogs.com/zxzmnh/p/11706819.html

时间: 2024-07-30 05:24:19

1129 Recommendation System PAT 甲级真题(输出格式,set使用)的相关文章

1020. Tree Traversals (25) PAT甲级真题

之前我看了这道题,实在是看不懂网上的解题答案,他们的具体思路基本上就是通过后续遍历和中序遍历,直接推出层次遍历. 我苦思冥想了半天,是在没看懂这种思路,于是想了一个笨点的但是也比较好理解的思路,通过后续和中序,先推出整个二叉树,再考虑 对二叉树层次遍历. 本题还有一点要注意的时在输出结果的末尾,如果使用了类似 pirntf("%d ",data); 这样的格式是不对的,一定要对末尾进行判断消除最尾端的空格. 首先最核心的部分是通过两次遍历反推回二叉树:这里的思路是,后续遍历的最末尾,一

1085. Perfect Sequence (25)-PAT甲级真题

1085. Perfect Sequence (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng Given a sequence of positive integers and another positive integer p. The sequence is said to be a "perfect sequence" if M <= m * p where M and m

1078. Hashing (25)-PAT甲级真题

1078. Hashing (25)The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be "H(key) = key % TSize" where TSize is the

PAT甲题题解-1111. Online Map (30)-PAT甲级真题(模板题,两次Dijkstra,同时记下最短路径)

题意:给了图,以及s和t,让你求s到t花费的最短路程.最短时间,以及输出对应的路径.   对于最短路程,如果路程一样,输出时间最少的. 对于最短时间,如果时间一样,输出节点数最少的.   如果最短路程和最短时间路径一样,合并输出一次即可. 纯粹就是练习dijkstra,没什么难的. 第一次dijkstra求最短路程,记录下每个节点的路程和时间. 第二次dijkstra求最短时间,记录下每个节点的时间和经过的节点数. pre数组用来存储前驱节点,保存路径 #include <iostream>

PAT 甲级真题题解(121-155)

1121 Damn Single 模拟 1 // 1121 Damn Single 2 #include <map> 3 #include <vector> 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 using namespace std; 8 9 map<int, int> m, vis; 10 vector<int> p; 11

1132 Cut Integer PAT 甲级真题

#include<iostream> #include<sstream>#include<string>using namespace std;int n;bool judge(string s){ long long t1, t2, t3; stringstream ss; /*ss.clear(); ss << s; ss >> t1; string s1 = s.substr(0,(int) s.size() / 2); ss.clear(

PAT 1129 Recommendation System[比较]

1129 Recommendation System(25 分) 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 ha

1054. 求平均值 (20)-PAT乙级真题

今天刚刚到学校,2017年学习正式开始了,今天看到了浙大的<数据结构>这学期又要开课了,决定一定要跟着学习一遍:在大学生mooc网上学习:http://www.icourse163.org/course/zju-93001#/info :然后就是跟着<算法之美>也要同步看完. 然后就在PAT上随便做一道题,这是第一次通过AC,发现了两个比较好的博客主页:http://www.liuchuo.net/  和  https://www.joyhwong.com/   都总结了刷题的过程

PAT 乙级真题 1002.数字分类

PAT 乙级真题 1002.数字分类 题目描述 给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...: A3 = 被5除后余2的数字的个数: A4 = 被5除后余3的数字的平均数,精确到小数点后1位: A5 = 被5除后余4的数字中最大数字. 输入格式 每个输入包含1个测试用例.每个测试用例先给出一个不超过1000的正整数N,随后给出N个不超过1000的