PAT_A1129#Recommendation System

时间:2019-05-31 21:44:32   收藏:0   阅读:161

Source:

PAT A1129 Recommendation System (25 分)

Description:

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

Keys:

Attention:

Code:

 1 /*
 2 Data: 2019-05-31 19:33:26
 3 Problem: PAT_A1129#Recommendation System
 4 AC: 01:04:23
 5 
 6 
 7 题目大意:
 8 推荐系统会预测用户对于商品的喜好程度,现在通过用户获取商品的次数来评估用户的喜好程度
 9 输入:
10 第一行给出,查询次数N<=5e4,系统给出推荐最大数量K<=10
11 第二行给出,用户依次查询的商品编号(1~N)
12 输出:
13 当前搜索编号:推荐商品编号 <=k
14 */
15 #include<cstdio>
16 #include<algorithm>
17 using namespace std;
18 const int M=5e5+10,N=10;
19 int rec[N],w[M]={0},in[M]={0},pt=0;
20 
21 bool cmp(const int &a, const int &b)
22 {
23     if(w[a]!=w[b])
24         return w[a]>w[b];
25     else
26         return a < b;
27 }
28 
29 int main()
30 {
31 #ifdef    ONLINE_JUDGE
32 #else
33     freopen("Test.txt", "r", stdin);
34 #endif
35 
36     int n,k,index;
37     scanf("%d%d", &n,&k);
38     for(int i=0; i<n; i++)
39     {
40         scanf("%d", &index);
41         if(i!=0)
42         {
43             printf("%d:", index);
44             for(int j=0; j<min(pt,k); j++)
45                 printf(" %d", rec[j]);
46             printf("\n");
47         }
48         w[index]++;
49         if(in[index]==0){
50             rec[pt]=index;
51             in[index]=1;
52             if(pt<k+1)  pt++;
53         }
54         sort(rec,rec+pt+1,cmp);
55         if(pt==k+1)
56             in[rec[pt]]=0;
57     }
58 
59     return 0;
60 }

 

原文:https://www.cnblogs.com/blue-lin/p/10957549.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!