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 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:
1 | 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:
1 | 12 3 |
Sample Output:
1 | 5: 3 |
作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
题目大意
给出客户的n个查询,以及最大推荐数k。要求客户查询时,向其推荐之前查询次数最多的k个商品。查询次数相同时,输出序号小的。
分析
利用一个showup数组记录id的出现次数。再用set数组times,记录出现次数为i的id。每次读入数字,计算最大出现次数,更新times。然后从times[maxtime]开始,输出k个数字即可。这题卡时间非常严重,一定要使用scanf/printf。
代码
1 |
|