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

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <set>
using namespace std;
const int maxn=;
struct node{
int num;
int cnt;
node(int num,int cnt):num(num),cnt(cnt){};
bool operator < (const node& a) const{
return cnt!=a.cnt?cnt>a.cnt:num<a.num;
}
};
set<node> s;
int times[];
int main(){
int n,k;
scanf("%d %d",&n,&k);
for(int i=;i<n;i++){
int tmp;
scanf("%d",&tmp);
if(i!=){
printf("%d:",tmp);
int j=;
for(auto it=s.begin();it!=s.end()&&j<k;it++,j++){
printf(" %d",it->num);
}
printf("\n");
}
if(s.find(node(tmp,times[tmp]))!=s.end()){
s.erase(s.find(node(tmp,times[tmp])));
}
times[tmp]++;
s.insert(node(tmp,times[tmp]));
}
}

注意点:用数组,然后再复制排序,后面3个测试点都会超时,这里要用set。虽然一开始想到的也是要用set,但set只能对数字自动排序,咋办,看了大佬的,原来可以重载<符号,这样set可以自动对结构体排序了,还有就是结构体的构造函数的写法,这样可以直接构造一个node了。之前看的primec++全忘完了。。。

超时代码:虽然写的时候就感觉会超时,但想不到别的办法,只好硬着头皮写了,考试的时候这样也能拿个16分,就酱吧,花不到30分钟还行

#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn=;
struct node{
int num;
int cnt;
}nodes[maxn];
bool cmp(node n1,node n2){
return n1.cnt==n2.cnt?n1.num<n2.num:n1.cnt>n2.cnt;
}
node res[maxn];
int main(){
int n,k;
int count=;
scanf("%d %d",&n,&k);
for(int i=;i<n;i++){
int tmp;
scanf("%d",&tmp);
if(i!=){
printf("%d:",tmp);
memcpy(res,nodes,sizeof(nodes));
sort(res,res+maxn,cmp);
for(int j=;j<k;j++){
if(res[j].cnt!=){
printf(" %d",res[j].num);
}
}
printf("\n");
}
nodes[tmp].num=tmp;
nodes[tmp].cnt++;
}
}

PAT A1129 Recommendation System (25 分)——set,结构体重载小于号的更多相关文章

  1. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

    1070 Mooncake (25 分)   Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...

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

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

  3. PAT 1129 Recommendation System[比较]

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

  4. PTA PAT排名汇总(25 分)

    PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...

  5. PAT甲级——A1129 Recommendation System【25】

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

  6. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

  7. PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

    1016 Phone Bills (25 分)   A long-distance telephone company charges its customers by the following r ...

  8. A1129. Recommendation System

    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. 转载 基于JAVA每月运势api调用代码实例

    代码描述:基于JAVA每月运势api调用代码实例 接口地址:http://www.juhe.cn/docs/api/id/58 原文链接:http://outofmemory.cn/code-snip ...

  2. 【Spring】23、ApplicationContext ,ApplicationContextAware,Listener,Event 的关系解读

    tomcat容器启动流程 启动tomcat容器,加载web.xml,建立整个容器(Servlet容器,这里是tomcat吧)的上下文,ServletContext,这时web.xml有个监听器,就是C ...

  3. Google Chrome 中安装 PostMan 扩展

    简介 PostMan 是调试 HTTP 请求的好工具,也是业界的佼佼者,这对于我们开发 Web Service 提供了很好的调试入口,支持请求认证机制.最关键的是,这个工具提供 Google Chro ...

  4. Docker第二章:docker基础1--镜像,容器&仓库

    镜像介绍及操作:http://www.haveneed.cn/article-detials/115 容器介绍及操作:http://www.haveneed.cn/article-detials/11 ...

  5. LearnX控件漏洞挖掘与利用

    前言 大学英语会用到一个 ActiveX 插件 LearnX ,最近从网上下了一个下来分析了一下,找到了一些漏洞并完成了 exploit . 虽然涉及的知识比较老旧,不过还是挺有意思的.这里分享一下整 ...

  6. Java并发编程(五)Lock

    一.synchronized的缺陷 synchronized是java中的一个关键字,也就是说是Java语言内置的特性.那么为什么会出现Lock呢? 在上面一篇文章中,我们了解到如果一个代码块被syn ...

  7. 51Testing专访史亮:测试人员在国外

    不久前,我接受了51Testing的访问,讨论了软件测试的一些问题.以下是全文. 1.史亮老师,作为我们51Testing的老朋友,能和我们说说您最近在忙些什么吗? 自2011年起,我加入Micros ...

  8. springboot 学习之路 20 (整合RabbitMQ)

    整合RabbitMQ: 我的操作系统是window7 ,所以在整合ribbotMQ之前需要先安装rabbitMq服务:安装步骤请参考:window下安装RabbitMQ  这个详细介绍了安装步骤,请按 ...

  9. JavaScript大杂烩11 - 理解事件驱动

    前面我们回顾了前端JavaScript只干的两件事:操作BOM与操作DOM,那么什么时候去干这些事呢?答案是需要干的时候去干.那么什么时候是需要干的时候呢?答案是事件被触发的时候.这就是通常所说的“事 ...

  10. Python之SGDRegressor

    实现: # -*- coding: UTF-8 -*- import numpy as npfrom sklearn.linear_model import SGDRegressor __author ...