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:

 /*
Data: 2019-08-11 21:13:07
Problem: PAT_A1129#Recommendation System
AC: 43:20 题目大意:
推荐系统会预测用户对于商品的喜好程度,现在通过用户获取商品的次数来评估用户的喜好程度
输入:
第一行给出,查询次数N<=5e4,系统给出推荐最大数量K<=10
第二行给出,用户依次查询的商品编号(1~N)
输出:
当前搜索编号:推荐商品编号 <=k
*/
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int M=1e6;
int h[M]={},t[M]={}; bool cmp(const int &a, const int &b)
{
if(t[a] != t[b])
return t[a] > t[b];
else
return a < b;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,k,x;
vector<int> recmd;
scanf("%d%d", &n, &k);
for(int i=; i<n; i++)
{
scanf("%d", &x);
if(i!=)
{
printf("%d: ", x);
for(int i=; i<recmd.size(); i++)
printf("%d%c", recmd[i], i==recmd.size()-?'\n':' ');
}
t[x]++;
if(recmd.size()<k && h[x]==)
{
recmd.push_back(x);
h[x]=;
}
else if(recmd.size()==k && h[x]==)
{
if(t[x]>t[recmd[k-]] || (t[x]==t[recmd[k-]] && x<recmd[k-]) )
{
h[recmd[k-]]=;
h[x]=;
recmd[k-]=x;
}
}
sort(recmd.begin(),recmd.end(),cmp);
} return ;
}

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. 洛谷——P1034 矩形覆盖

    https://www.luogu.org/problem/show?pid=1034 题目描述 在平面上有 n 个点(n <= 50),每个点用一对整数坐标表示.例如:当 n=4 时,4个点的 ...

  2. ArcEngine控制台应用程序

    转自wbaolong原文 ArcEngine控制台应用程序 控制台应用程序相比其他应用程序,更加简单,简化了许多冗余,可以让我们更加关注于本质的东西. 现在让我们看一看ArcGIS Engine的控制 ...

  3. HDU 5433

    每次BC都好心酸... BFS+queue..状态可以设为p_val[x][y][k],加上斗志的值. #include <iostream> #include <cstdio> ...

  4. lsyncd + rsync 实时同步搭建

    一.inotify和lsync inotify和lsyncd对比一下,发现虽然lsyncd没有inotify那么真正的实时同步,但是lsyncd的同步基本上可以满足基本实时同步的要求,而且lsyncd ...

  5. but no declaration can be found for element &#39;aop:aspectj-autoproxy&#39;.

    1.错误描写叙述 Multiple annotations found at this line: - cvc-complex-type.2.4.c: The matching wildcard is ...

  6. 技术总结--android篇(四)--工具类总结

    StringUtil(视个人须要进行加入) public class StringUtil { public static boolean isMail(String string) { if (nu ...

  7. Rust 中项目构建管理工具 Cargo简单介绍

    cargo是Rust内置的项目管理工具.用于Rust 项目的创建.编译.执行,同一时候对项目的依赖进行管理,自己主动推断使用的第三方依赖库,进行下载和版本号升级. 一.查看 cargo 版本号 安装R ...

  8. SolrCloud 分布式集群部署步骤

    https://segmentfault.com/a/1190000000595712 SolrCloud 分布式集群部署步骤 solr solrcloud zookeeper apache-tomc ...

  9. UVA LIVE 7146 Defeat the Enemy

    这个题跟codeforces 556 D Case of Fugitive思路一样 关于codeforces 556 D Case of Fugitive的做法的链接http://blog.csdn. ...

  10. Java8新特性之Optional

    空指针异常一直是困扰Java程序员的问题,也是我们必须要考虑的.当业务代码中充满了if else判断null 的时候程序变得不再优雅,在Java8中提供了Optional类为我们解决NullPoint ...