PAT1129:Recommendation System
1129. Recommendation System (25)
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的更多相关文章
- 海量数据挖掘MMDS week4: 推荐系统Recommendation System
http://blog.csdn.net/pipisorry/article/details/49205589 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...
- A1129. Recommendation System
Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...
- PAT A1129 Recommendation System (25 分)——set,结构体重载小于号
Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...
- 1129 Recommendation System
1129 Recommendation System (25 分) Recommendation system predicts the preference that a user would gi ...
- PAT甲级 1129. Recommendation System (25)
1129. Recommendation System (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT 1129 Recommendation System[比较]
1129 Recommendation System(25 分) Recommendation system predicts the preference that a user would giv ...
- PAT 甲级 1129 Recommendation System
https://pintia.cn/problem-sets/994805342720868352/problems/994805348471259136 Recommendation system ...
- 1129 Recommendation System (25 分)
Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...
- PAT 1129 Recommendation System
Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...
随机推荐
- 【一天一道LeetCode】#22. Generate Parentheses
一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of we ...
- Linux网络设置(第二版) --互联网寻址过程
Linux网络设置 --互联网寻址过程 1.TCP/IP与OSI参考模型比较 TCP/IP OSI 物理层 网卡 数据链路层 * MAC地址 网络层 IP,ICMP,ARP协议 传输层 TCP,UDP ...
- Android虚拟设备访问WebSocket问题
Android虚拟设备访问WebSocket问题 最近写erlang的WebSocket网站,需要运行在RHEL6上,用Android设备访问. 可惜AVD无法访问主机 Win7上的虚拟机(RHEL6 ...
- Linux内核中断和异常分析(下)
这节,我们继续上,中(以前的日志有)篇目进行分析,结合一个真实的驱动案例来描述linux内核中驱动的中断机制,首先我们先了解一下linux内核中提供的中断接口. 这个接口我们需要包含一个头文件:#in ...
- obj-c编程10:Foundation库中类的使用(3)[文件管理]
好吧,不管神马系统都无可避免的要说到文件,目录,路径(PATH)管理的内容,下面我们来看看在F库中对他们的支持.我简单看了下,不谈其他光从方法命名来说就多少显得有点复杂,如果和ruby相比就呵呵了. ...
- App 被拒 -- App Store Review Guidelines (2015)中英文对照
Introduction(简介) We're pleased that you want to invest your talents and time to develop applications ...
- 百度java开发面试题
第一面 项目: 1.找一个项目,介绍下情况.其中遇到了什么问题,每种问题怎么样的解决方案. 算法题: 2.一个排好序的数组,找出两数之和为m的所有组合 3.自然数序列,找出任意连续之和等于n ...
- rcp perspective 添加9个视图均匀排列
PageLayout布局方法 pageLayout.addView("NodePMTSStream" + ":1", IPageLayout.TOP, 0.5f ...
- IBM RAD 快捷键
Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ ...
- 关于MVC的特性(AuthorizeAttribute)的一些理解
许多 Web 应用程序要求在用户登录之后才授予其对受限制内容的访问权限. 在某些应用程序中,即使是登录的用户,也会限制他们可以查看的内容或可以编辑的字段. 要限制对 ASP.NET MVC 视图的访问 ...