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++;
}
}

最新文章

  1. UE4 AI BehaviorTree 各个节点执行顺序总结
  2. apache 局域网访问
  3. java开发环境搭建
  4. VPS/服务器优化网络、加速方法总结与参考
  5. Windows Azure 负载均衡会话保持
  6. [ASP.NET]动态绑定树控件:
  7. MVC bundle(CSS或JS)
  8. openerp import namespace
  9. try/catch异常捕捉
  10. nginx自动切割访问日志
  11. 服务器端开发(Python/C++)-今日头条-拉勾网-最专业的互联网招聘平台
  12. git 不成功
  13. 22 , CSS 构造颜色、背景与图像
  14. unittest的使用二——生成基于html的测试报告
  15. 【python】如何将ipdb的python解释器路径切换至虚拟环境中
  16. Java中Jdom解析XML
  17. 剑指offer-在数组中查找两个数,是的他们的和正好是S(一次性跑通)(时间复杂度还可以降低)
  18. 一些jquery常用方法
  19. java性能分析工具 jconsole.exe
  20. 图像处理之中值滤波介绍及C实现

热门文章

  1. 了解java虚拟机—G1回收器(9)
  2. blfs(systemv版本)学习笔记-安装、配置和使用wpa_supplicant无线网络连接工具
  3. windows使用笔记-安装64位windows7家庭普通版的方法
  4. mysql索引类型 normal, unique, full text
  5. HTML5文件API之FileReader
  6. c#权限验证
  7. Cannot find wrapper assembly for type library &quot;ADODB&quot;. in VS2017
  8. HDMI驱动热插拔检测方法
  9. iOS 多线程之GCD的简单使用
  10. zabbix监控磁盘IO