A1129. Recommendation System
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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
typedef struct NODE{
int id;
int cnt;
}info;
bool operator <(const info &a, const info &b){
if(a.cnt == b.cnt)
return a.id < b.id;
else return a.cnt > b.cnt;
}
set<info> st;
int hashTB[] = {};
int main(){
int N, K, qu;
scanf("%d%d%d", &N, &K, &qu);
info temp = {qu, };
st.insert(temp);
hashTB[qu]++;
for(int i = ; i < N; i++){
scanf("%d", &qu);
printf("%d:", qu);
set<info>::iterator itp;
int prt;
for( prt = , itp = st.begin(); prt < K && itp != st.end(); prt++, itp++){
printf(" %d", itp->id);
}
printf("\n");
info nd = {qu, hashTB[qu]};
set<info>::iterator it = st.find(nd);
if(it != st.end()){
st.erase(it);
}
nd.cnt = ++hashTB[qu];
st.insert(nd);
}
cin >> N;
return ;
}
总结:
1、题意:动态统计次数。每当用户购买一个东西时,从他购买该东西之前的购买历史中统计出高买次数最多的前K个产品,并推荐给他。
2、N很大,所以每次都排序肯定超时。 由于set可以保证内部有序,所以使用set来排序,用hash表来统计出现频率。每次新购一个物品,先去set中查找,如果存在则删除再重新插入更新后的,如果不存在则直接插入。
3、set重载小于号,在struct外:
bool operator <(const info &a, const info &b){
if(a.cnt == b.cnt)
return a.id < b.id;
else return a.cnt > b.cnt;
}
4、set使用find函数查找 struct 类型变量时,由于已经重载了小于号,所以set会利用小于号来判断不等于。所以可以正常传入一个struct变量 a 并在find函数中查找成员变量都和a相等的变量。
5、给分最多的几个测试点好像用时在2ms左右,所以到时候实在想不到就暴力破解。
A1129. Recommendation System的更多相关文章
- PAT A1129 Recommendation System (25 分)——set,结构体重载小于号
Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...
- PAT甲级——A1129 Recommendation System【25】
Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...
- PAT_A1129#Recommendation System
Source: PAT A1129 Recommendation System (25 分) Description: Recommendation system predicts the prefe ...
- 海量数据挖掘MMDS week4: 推荐系统Recommendation System
http://blog.csdn.net/pipisorry/article/details/49205589 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...
- PAT1129:Recommendation System
1129. Recommendation System (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 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 ...
随机推荐
- css3 text-shadow字体阴影讲解
text-shadow:为字体添加阴影, 可以通过对text-shadow属性设置相关的属性值,来实现现一些需要的字体阴影效果,减少了图片的使用. 基础说明: text-shadow: X轴 ...
- 转:Flutter Decoration背景设定(边框、圆角、阴影、形状、渐变、背景图像等)
1 继续关系: BoxDecoration:实现边框.圆角.阴影.形状.渐变.背景图像 ShapeDecoration:实现四个边分别指定颜色和宽度.底部线.矩形边色.圆形边色.体育场(竖向椭圆). ...
- Linux 文件及目录管理命令基础
pwd 显示当前所在目录 cd 切换目录 cd 命令语法 cd [选项] 目录 cd 的常用选项: cd ~ /cd 切换到当前用户的加目录 cd . 保持当前目录不变 cd .. 切换到上级目录 ...
- 【gedit】 显示行号
打开gedit文本编辑器->Edit(编辑)->preferences(预设)->view(视图)->在Display line numbers前打勾->close
- RuntimeError: cryptography requires setuptools 18.5 or newer, please upgrade to a newer version of setuptool
setuptool 太老了,更新下: pip install --upgrade setuptools
- word2vec训练&IC分词(待)
参考http://www.52nlp.cn/%E4%B8%AD%E8%8B%B1%E6%96%87%E7%BB%B4%E5%9F%BA%E7%99%BE%E7%A7%91%E8%AF%AD%E6%96 ...
- 魔术方法:__set、__get
__set: 在设置对象里边不能直接设置(或没有)的属性值的时候,自动去被调用 class Track { private $track_name; public function __set($na ...
- C# 23种设计模式汇总
创建型模式工厂方法(Factory Method)在工厂方法模式中,工厂方法用来创建客户所需要的产品,同时还向客户隐藏了哪种具体产品类将被实例化这一细节.工厂方法模式的核心是一个抽象工厂类,各种具体工 ...
- 吴恩达deeplearning之CNN—卷积神经网络
https://blog.csdn.net/ice_actor/article/details/78648780 个人理解: 卷积计算的过程其实是将原始的全连接换成了卷积全连接,每个kernel为对应 ...
- CentOS6.5内核编译
内核源码包下载地址,戳我 1.准备并解压内核安装包:linux-4.14.6.tar.xz # .tar.xz -C /usr/src/ # cd /usr/src/linux- #查看linux-目 ...