PAT甲级——A1129 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 (≤ 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
//题目大意:就是顾客买东西之前,根据顾客的购买历史进行商品推荐
//所以购买第一件商品3是没有推荐的,因为没有购买历史记录
//然后根据前面购买的历史次数,进行次数从多到少进行推荐,最多推荐三个,相同次数的推荐标号最小的的//
//使用set来得到按要求的排序规格,vector来辅助查询set中是否已经存在元素
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int n, k;
struct Node
{
int value, cnt;
bool operator < (const Node& a)const//set的排序规则
{
return (cnt != a.cnt) ? cnt > a.cnt:value < a.value;
}
};
int main()
{
int n, k, num;
scanf("%d %d", &n, &k);
set<Node>s;
vector<int>v(n + , );
for (int i = ; i < n; ++i)
{
cin >> num;
if (i != )//第一件商品没有推荐
{
cout << num << ":";
int i = ;
for (auto ptr = s.begin(); ptr != s.end() && i <= k; ++ptr, i++)
cout << " " << ptr->value;
cout << endl;
}
auto ptr = s.find(Node{ num,v[num] });
if (ptr != s.end())//记住set不能修改内容,只能擦除后重新写入
s.erase(ptr);
v[num]++;
s.insert(Node{ num,v[num] });//使用了初始化列表原则
}
return ;
}
PAT甲级——A1129 Recommendation System【25】的更多相关文章
- PAT甲级 1129. Recommendation System (25)
1129. Recommendation System (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT 甲级 1129 Recommendation System
https://pintia.cn/problem-sets/994805342720868352/problems/994805348471259136 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 ...
- A1129. Recommendation System
Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...
- 1129. Recommendation System (25)
Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...
- 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)
题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...
- PAT甲题题解-1129. Recommendation System (25)-排序
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789819.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级 1122. Hamiltonian Cycle (25)
1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- PAT甲级 1121. Damn Single (25)
1121. Damn Single (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue "Dam ...
随机推荐
- java mysql 数据库
1. jdbc 驱动名还是数据库 String driver = "com.mysql.jdbc.Driver"; //URL指向要访问的数据库名mydataString url ...
- jq-demo-阻止冒泡,阻止默认行为
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 自行制作yum源仓库
背景 客户服务器为内网机器,centos7系统,且无法与外网连接.需要部署对应的LANMP环境及其它软件 解决思路 1.在阿里云服务器,利用阿里云的yum源仓库,下载对应软件及关联软件. 2.在客户机 ...
- Batch - %~dp0 modifiers
%~dp0 简易解释 The variable %0 in a batch script is set to the name of the executing batch file. The ~dp ...
- Delphi Format函数功能及用法详解
DELPHI中Format函数功能及用法详解 DELPHI中Format函数功能及用法详解function Format(const Format: string; const Args: array ...
- 牛客多校第六场 G Is Today Friday? 蔡勒公式/排列
题意: 有一堆日期,这些日期都是星期五,但是数字被映射成了字母A~J,现在让你求逆映射,如果存在多种答案,输出字典序最小的那个. 题解: 用蔡勒公式解决关于星期几的问题. 对于映射,可以用笔者刚刚学会 ...
- Java-Class-@I:org.junit.runner.RunWith
ylbtech-Java-Class-@I:org.junit.runner.RunWith 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 1. package org.juni ...
- CVE-2017-3248简单复现
我是这样操作的 目标跟windows在一个段,linux是另一个段的,我的虚拟机 windows主机上 `java -cp ysoserial.jar ysoserial.exploit.JRMPLi ...
- Rabbit MQ 基础入门
Rabbit MQ 学习(一)基础入门 简介 RabbitMQ 简介 为什么选择 RabbitMQ RabbitMQ 的模型架构是什么? AMQP 协议是什么? AMQP 常用命令 概念 生产者和消费 ...
- iOS开发系列-HTTPS
HTTPS 网景在1994年创建了HTTPS,并应用在网景导航者浏览器中. 最初,HTTPS是与SSL一起使用的:在SSL逐渐演变到TLS. HTTPS协议与HTTP协议的一些不同: http是超文本 ...