PAT_A1129#Recommendation System

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:

  • 在线处理
  • cmp引用传参
  • 其实运算符重载也是可以的

Code:

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

 

posted @ 2019-05-31 21:12  林東雨  阅读(132)  评论(0编辑  收藏  举报