PAT (Advanced Level) Practice 1129 Recommendation System (25 分) 凌宸1642

PAT (Advanced Level) Practice 1129 Recommendation System (25 分) 凌宸1642

题目描述:

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.

译:每个输入文件包含一个测试用例。 对于每个测试用例,第一行包含两个正整数:N (≤ 50,000),查询总数和 K (≤ 10),系统必须向用户显示的最大推荐数。 然后在第二行给出用户正在访问的项目的索引——为了简单起见,所有项目的索引从 1 到 N。一行中的所有数字都用空格分隔。


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.

译:对于每种情况,逐一处理查询。 以以下格式在一行中输出每个查询的建议:

query: rec[1] rec[2] ... rec[K]

其中 query 是用户正在访问的项目,rec[i] (i=1, ... K) 是系统向用户推荐的第 i 个项目。 最常访问的前 K 个项目应该按照其频率的非递增顺序进行推荐。 如果有平局,则项目将按其索引升序排列。

注意:第一项没有输出,因为当时无法给出任何建议。 保证至少有一个查询的输出。


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

The Idea:

  • 题目意思很明显,我们需要给已经访问过得数据一个计数器,每次推荐计数器更大的,如果计数器的值一样,推荐 value值更小的
  • 可以设计一个结构体 node ,存储每个 node 出现的次数以及 value 值,然后将其用 set 容器存储,重载 结构体 node 的 < 运算符。

The Codes:

#include<bits/stdc++.h>
using namespace std ;
int book[50001];
struct node {
	int value, cnt;
	bool operator < (const node &a) const {
		return (cnt != a.cnt) ? cnt > a.cnt : value < a.value;
	}
};
int main() {
	int n, k, num;
	cin >> n >> k ;
	set<node> s;
	for (int i = 0; i < n; i++) {
		cin >> num ;
		if (i != 0) {
			cout << num << ":";
			int tempCnt = 0;
			for(auto it = s.begin(); tempCnt < k && it != s.end(); it++) {
				cout << ' ' << it->value ;
				tempCnt++;
			}
			cout << endl ;
		}
		auto it = s.find(node{num, book[num]});
		if (it != s.end()) s.erase(it);
		book[num]++;
		s.insert(node{num, book[num]});
	}
	return 0 ;
}

posted @ 2021-08-16 13:45  凌宸1642  阅读(37)  评论(0编辑  收藏  举报