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 (≤ 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:

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.

知识点:STL的使用,排序

思路:每一次输入后,都要让所有元素有序。如果利用线性结构,那么他的时间复杂度是O(n),在数据量大的时候也是不行的。利用STL的set,插入的时间复杂度降为O(logN)

 #include <cstdio>
#include <set>
using namespace std;
const int maxn = ; int clickTime[maxn];
struct nodetype{
int v,cnt;
nodetype(int a,int b) : v(a), cnt(b) {}
bool operator < (const nodetype &a) const {
return (cnt!=a.cnt)?cnt>a.cnt:v<a.v;
}
}; int main(int argc, char *argv[]) {
fill(clickTime,clickTime+maxn,); int n,k,tmp;
scanf("%d %d",&n,&k);
set<nodetype> s;
for(int i=;i<=n;i++){
scanf("%d",&tmp);
if(i!=){
printf("%d:",tmp);
int cnt=;
for(auto it=s.begin();cnt<k&&it!=s.end();it++){
printf(" %d",it->v);
cnt++;
}
printf("\n");
} auto it = s.find(nodetype(tmp,clickTime[tmp]));
if(it!=s.end()){
s.erase(nodetype(tmp,clickTime[tmp]));
}
s.insert(nodetype(tmp,clickTime[tmp]+));
clickTime[tmp]++;
}
}

1129 Recommendation System的更多相关文章

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

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

  2. PAT 1129 Recommendation System[比较]

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

  3. PAT 甲级 1129 Recommendation System

    https://pintia.cn/problem-sets/994805342720868352/problems/994805348471259136 Recommendation system ...

  4. 1129 Recommendation System (25 分)

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

  5. PAT 1129 Recommendation System

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

  6. 1129. Recommendation System (25)

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

  7. PAT甲题题解-1129. Recommendation System (25)-排序

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789819.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. PAT1129:Recommendation System

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

  9. 海量数据挖掘MMDS week4: 推荐系统Recommendation System

    http://blog.csdn.net/pipisorry/article/details/49205589 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...

随机推荐

  1. MVC中Ajax post 和Ajax Get——提交对象

    HTTP 请求:GET vs. POST两种在客户端和服务器端进行请求-响应的常用方法是:GET 和 POST.GET - 从指定的资源请求数据POST - 向指定的资源提交要处理的数据GET 基本上 ...

  2. golang语言中os/signal包的学习与使用

    package main; import ( "os" "os/signal" "fmt" ) //signal包中提供了两个函数 //No ...

  3. 2Y - sort

    给你n个整数,请按从大到小的顺序输出其中前m大的数.  Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-5000 ...

  4. PAT 1006 换个格式输出整数 (15)(C++&JAVA&Python)

    1006 换个格式输出整数 (15)(15 分) 让我们用字母B来表示"百".字母S表示"十",用"12...n"来表示个位数字n(& ...

  5. [Jmeter] Run Command to generate a specific listener’s chart report

    Run Command to generate a specific listener’s chart report: Download cmdrunner-2.0.jar : https://jme ...

  6. unwind

    unwind:可以将一个列表展开为一个行的序列1.列表 unwind[1,2,3]as x return x2.创建唯一列表with[1,2,3,3]as coll unwind coll as x ...

  7. Android Studio 小新兵

    1. java.lang.IllegalStateException: This app has been built with an incorrect configuration. Please ...

  8. 【搜索】Dungeon Master

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  9. [html]Sublime Text添加插件

    今天想在Sublime Text(简称ST)内编写HTML后直接使用浏览器看效果,想添加View in Browser插件,然后遇到奇怪的问题添加插件直接报"找不到有用的插件" 一 ...

  10. ssh设置无密码登录

    设置无密码登录此处设为有主机a登录到主机b 1.在主机a生成公钥 ssh-keygen -t rsa  之后有导航(其实一直回车就可以) 2.此时在主机a/home/YOURHOSTNAME/.ssh ...