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:

  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
  2. 3 5 7 5 5 3 2 1 8 3 8 12

Sample Output:

  1. 5: 3
  2. 7: 3 5
  3. 5: 3 5 7
  4. 5: 5 3 7
  5. 3: 5 3 7
  6. 2: 5 3 7
  7. 1: 5 3 2
  8. 8: 5 3 1
  9. 3: 5 3 1
  10. 8: 3 5 1
  11. 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. 第一行给出,查询次数N<=5e4,系统给出推荐最大数量K<=10
  10. 第二行给出,用户依次查询的商品编号(1~N)
  11. 输出:
  12. 当前搜索编号:推荐商品编号 <=k
  13. */
  14. #include<cstdio>
  15. #include<vector>
  16. #include<algorithm>
  17. using namespace std;
  18. const int M=1e6;
  19. int h[M]={},t[M]={};
  20.  
  21. bool cmp(const int &a, const int &b)
  22. {
  23. if(t[a] != t[b])
  24. return t[a] > t[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 // ONLINE_JUDGE
  35.  
  36. int n,k,x;
  37. vector<int> recmd;
  38. scanf("%d%d", &n, &k);
  39. for(int i=; i<n; i++)
  40. {
  41. scanf("%d", &x);
  42. if(i!=)
  43. {
  44. printf("%d: ", x);
  45. for(int i=; i<recmd.size(); i++)
  46. printf("%d%c", recmd[i], i==recmd.size()-?'\n':' ');
  47. }
  48. t[x]++;
  49. if(recmd.size()<k && h[x]==)
  50. {
  51. recmd.push_back(x);
  52. h[x]=;
  53. }
  54. else if(recmd.size()==k && h[x]==)
  55. {
  56. if(t[x]>t[recmd[k-]] || (t[x]==t[recmd[k-]] && x<recmd[k-]) )
  57. {
  58. h[recmd[k-]]=;
  59. h[x]=;
  60. recmd[k-]=x;
  61. }
  62. }
  63. sort(recmd.begin(),recmd.end(),cmp);
  64. }
  65.  
  66. return ;
  67. }

PAT_A1129#Recommendation System的更多相关文章

  1. 海量数据挖掘MMDS week4: 推荐系统Recommendation System

    http://blog.csdn.net/pipisorry/article/details/49205589 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...

  2. PAT1129:Recommendation System

    1129. Recommendation System (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  3. A1129. Recommendation System

    Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...

  4. PAT A1129 Recommendation System (25 分)——set,结构体重载小于号

    Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...

  5. 1129 Recommendation System

    1129 Recommendation System (25 分) Recommendation system predicts the preference that a user would gi ...

  6. PAT甲级 1129. Recommendation System (25)

    1129. Recommendation System (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  7. PAT 1129 Recommendation System[比较]

    1129 Recommendation System(25 分) Recommendation system predicts the preference that a user would giv ...

  8. PAT 甲级 1129 Recommendation System

    https://pintia.cn/problem-sets/994805342720868352/problems/994805348471259136 Recommendation system ...

  9. 1129 Recommendation System (25 分)

    Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...

随机推荐

  1. CODEVS——T 1036 商务旅行

    http://codevs.cn/problem/1036/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Descript ...

  2. 调用BPL包中的函数

    BPL就是一种DLL,DLL的EXPORTS和GETPROCADDRESS()在BPL中一样好使. 要调用BPL中的方法也和调用DLL的一样.代码略.

  3. iOS:去除UITableView的空白行

    要去除UITableView在运行时显示的多余空白行,只需要将TableView的Style从Plain改为Grouped即可.

  4. ASP.NET MVC 提供与訪问 Web Api

    ASP.NET MVC 提供与訪问 Web Api 一.提供一个 Web Api 新建一个项目.类型就选 "Web Api". 我用的是MVC5,结果生成的项目一大堆东西.还编译只 ...

  5. Python图像处理库PIL中图像格式转换(一)

    在数字图像处理中,针对不同的图像格式有其特定的处理算法. 所以,在做图像处理之前,我们须要考虑清楚自己要基于哪种格式的图像进行算法设计及事实上现.本文基于这个需求.使用python中的图像处理库PIL ...

  6. C语言之基本算法40—字符串删除元音字母倒序输出

    //字符串,数组 /* ================================================================== 题目: 输入一行字符,将辅音字母按反序输出 ...

  7. C++学习笔记22,普通函数重载(1)

    转载请注明出处:http://blog.csdn.net/qq844352155/article/details/31353325 该博文仅用于交流学习,请慎用于不论什么商业用途,本博主保留对该博文的 ...

  8. React Native布局实践:开发京东client首页(三)——轮播图的实现

    上篇文章中,我们一起构建了京东client的TabBar.在本文中.将继续向大家介绍京东client首页轮播图及其下发功能button的开发方法,如今就让我们開始吧! 1.相关控件调研 眼下在Gith ...

  9. 查看服务器wwn是否在交换机侧

    判断port_state是否为Online状态,是的话,读取出port_name,即为wwn. #!/usr/bin/env python3 # -*- coding: UTF-8 -*- impor ...

  10. 第2章 安装Nodejs 2-2 Nodejs版本常识