1129. Recommendation System (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<= 50000), 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 思路
1.题目要求每次用户点击(输入)一个商品时,要把它之前点击次数前k大的商品打印出来。
2.可以构建一个item类并使用set容器的自动排序完成。item的"<"运算符需要重载,使其满足在set内部的排序条件如下:
1)点击次数越多的item越靠前
2)如果两个item点击次数相同,则比较它们的编号,编号小的靠前。 代码
#include<iostream>
#include<vector>
#include<set>
using namespace std;
vector<int> appear_time(50001,0);
class item
{
public:
int val;
int cnt;
item(int _val,int _cnt){val = _val;cnt = _cnt;}
bool operator <(const item& a) const
{
return cnt == a.cnt? val < a.val:cnt > a.cnt;
}
}; int main()
{
int n,k;
set<item> items;
while(cin >> n >> k)
{
item tmp(0,1);
cin >> tmp.val;
appear_time[tmp.val]++;
items.insert(tmp);
for(int i = 1;i < n;i++)
{
int a;
cin >> a;
cout << a <<":";
int index = 0;
for(set<item>::iterator it = items.begin();index < k && it != items.end();it++,index++)
cout << " " << it->val;
cout << endl;
auto iter = items.find(item(a,appear_time[a]));
appear_time[a]++;
if(iter == items.end())
items.insert(item(a,1));
else
{
items.erase(iter);
items.insert(item(a,appear_time[a]));
}
}
}
}

  

PAT1129:Recommendation System的更多相关文章

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

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

  2. A1129. Recommendation System

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

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

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

  4. 1129 Recommendation System

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

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

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

  6. PAT 1129 Recommendation System[比较]

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

  7. PAT 甲级 1129 Recommendation System

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

  8. 1129 Recommendation System (25 分)

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

  9. PAT 1129 Recommendation System

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

随机推荐

  1. C语言实现ifconfig获取网卡接收和发送流量统计

    在Windows下我们可以利用ipconfig命令获取网卡的相关信息,在Linux下命令是ifconfig 我们可以获取的信息更为丰富,其中包括网卡接收和发送的流量,用C语言实现这个命令并不是一件简单 ...

  2. Android中让多个线程顺序执行探究

    线程调度是指按照特定机制为多个线程分配CPU的使用权. 有两种调度模型:分时调度模型和抢占式调度模型. 分时调度模型:是指让所有的线程轮流获得cpu的使用权,并且平均分配每个线程占用的CPU的时间片. ...

  3. Kotlin For Android 示例代码实战

    下面就为大家介绍怎么使用Kotlin来开发Android 上面这篇中我们在下载Kotlin插件的时候也下了一个功能扩张插件,其实这个插件大有用处,它可以使得我们在不使用注解和第三方库的情况下不使用fi ...

  4. PDA开发数据由本地上传至DB

    private void btnUpLoad_Click(object sender, EventArgs e) { if (!System.IO.File.Exists(LoadFile)) { M ...

  5. 我所犯的JavaScript引用错误

    近期在w3cschool学习JavaScript和php--学完后,开始帮一哥们友情写网站.但是在使用ajax和Jquery的时候发现,我自己写的脚本不能运行.捣鼓了半天,没有发现任何语句错误.调试器 ...

  6. Android驱动中的Kconfig文件与Makefile文件

    内核源码树的目录下都有两个文档Kconfig(2.4版本是Config.in)和Makefile.分布到各目录的Kconfig构成了一个分布式的内核配置数据库,每个Kconfig分别描述了所属目录源文 ...

  7. thrift实现HDFS文件操作

    thrift 文件如下 namespace java com.pera.file.transform struct  File{     1:string path ,     2:string co ...

  8. Select、Poll与Epoll比较

    (1)select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当select()返回后,该数组中就绪的文件描述符便会被内核修改标志 ...

  9. JVM内存详解-阅读笔记

  10. nginx for Windows

    zt from nginx official site. Known issuesPossible future enhancements Version of nginx for Windows u ...