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:

  • 有点在线处理的意思,不太好想
  • 推荐列表要多开两个单位,k号必须是除了列表外,次数最多且序号最小的,因此还需要存储k+1号来保证k号的序号是最小的
  • cmp引用传参会快一点,没改之前有个测试点过不去,为此还打算自己重写排序算法,没想到试了下引用传参就好了0,0
  • 其实运算符重载也是可以的,最开始怕超时,没敢用,而且思路上更简单一些;

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值