pat 1129. Recommendation System (25)(set的增删)
来源:互联网 发布:香港买mac口红多少钱 编辑:程序博客网 时间:2024/06/05 16:37
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 (<= 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 33 5 7 5 5 3 2 1 8 3 8 12Sample Output:
5: 37: 3 55: 3 5 75: 5 3 73: 5 3 72: 5 3 71: 5 3 28: 5 3 13: 5 3 18: 3 5 112: 3 5 8
题意:给一列数,每次询问前m项出现次数最多的数字,出现次数相同按数字大小排序;
解:直接开个set在增加前做删除操作就可以了
#include<iostream>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<vector>#include<map>#include <set>//#include <bits/stdc++.h>using namespace std;const int N = 520;typedef long long LL;struct node{ int v, num; bool operator <(const node &A)const { if(num!=A.num) return num>A.num; else return v<A.v; }};set<node>st;set<node>::iterator it;int vis[50010];int main(){ st.clear(); int n, m; memset(vis,0,sizeof(vis)); scanf("%d %d", &n, &m); int x; scanf("%d", &x); vis[x]++; node tmp; tmp.v=x,tmp.num=vis[x]; st.insert(tmp); for(int i=2; i<=n; i++) { scanf("%d", &x); printf("%d:",x); int h=0, y; for(it=st.begin(); it!=st.end(); it++) { h++; printf(" %d",(*it).v); if(h==m) break; } vis[x]++; tmp.v=x,tmp.num=vis[x]-1; if(st.find(tmp)!=st.end())st.erase(tmp); tmp.num++; st.insert(tmp); printf("\n"); } return 0;}
- pat 1129. Recommendation System (25)(set的增删)
- PAT 1129. Recommendation System (25)
- 1129. Recommendation System (25) set
- PAT--1129. Recommendation System
- PAT甲级 1129. Recommendation System (25)
- PAT 1129. Recommendation System (25) 数数
- 【PAT】【Advanced Level】1129. Recommendation System (25)
- PAT 甲级 1129. Recommendation System (25)
- 1129. Recommendation System (25)[set集合-排序]
- PAT(A)-1129. Recommendation System (25)(优先队列)
- 1129. Recommendation System (25)
- 1129. Recommendation System (25)
- 1129. Recommendation System (25)
- 1129. Recommendation System (25)
- 1129. Recommendation System (25)
- 1129. Recommendation System (25)
- 1129. Recommendation System (25)
- PAT 1129Recommendation System
- Gaussian high-pass filter
- POJ 1458
- 1007:DNA排序
- System Properties Comparison Ehcache vs. Memcached vs. Redis Please select another system to includ
- 计算机顶级会议Rankings && 英文投稿的一点经验
- pat 1129. Recommendation System (25)(set的增删)
- 站长友联,网站推广的,有网站的进来看看了!!
- 如何搜索某个学科领域的文献综述?
- UVA 524
- Javrvis算法计算凸包的周长
- 反编译
- 解决MySQL登录ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwor)问题
- 分布式服务的事务如何处理
- 闪银奇异安全负责人:互金行业安全建设的四个心得