PAT--1129. Recommendation System
来源:互联网 发布:java最新面试题及答案 编辑:程序博客网 时间:2024/05/16 18:15
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 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
题解
题目是要对于每一个items[i], i > 1
,在前i - 1
个 items 中 输出访问次数最多的前k个item,如果次数相同,编号更小的优先。
访问次数可以用一个map
统计。 indices
和其访问次数cnt
可作为map
或者set
的key
,自定义其比较函数即可。
#include <iostream>#include <algorithm>#include <cstdio>#include <map>#include <cstring>using namespace std;const int maxn = 50000 + 5;int n, k;int items[maxn];struct Item{ int value; int cnt; Item(int v, int c):value(v), cnt(c){}};bool operator < (const Item& a, const Item& b){ if(a.cnt == b.cnt) return a.value < b.value; return a.cnt > b.cnt;}int main(){#ifndef ONLINE_JUDGEfreopen("data.in", "r", stdin);#endif // ONLINE_JUDGE cin >> n >> k; for(int i = 0; i < n; ++i) cin >> items[i]; map<int, int> mp1; map<Item, int> mp2; mp1[items[0]]++; mp2.insert(make_pair(Item{items[0], 1}, 1)); for(int i = 1; i < n; ++i){ int j = 0; cout << items[i] << ": "; for(auto it = mp2.begin(); it != mp2.end() && j < k; ++it, j++){ if(j) cout << " "; cout << it->first.value; } cout << endl; mp1[items[i]]++; int cnt = mp1[items[i]]; if(cnt == 1) mp2.insert(make_pair(Item{items[i], cnt}, 1)); else{ auto oldkey = mp2.find(Item{items[i], cnt - 1}); mp2.erase(oldkey); mp2.insert(make_pair(Item{items[i], cnt}, 1)); } } return 0;}
- PAT--1129. Recommendation System
- PAT 1129. Recommendation System (25)
- PAT甲级 1129. Recommendation System (25)
- PAT 1129. Recommendation System (25) 数数
- 【PAT】【Advanced Level】1129. Recommendation System (25)
- PAT 甲级 1129. Recommendation System (25)
- PAT 1129Recommendation System
- PAT(A)-1129. Recommendation System (25)(优先队列)
- pat 1129. Recommendation System (25)(set的增删)
- 1129. Recommendation System 解析
- 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) set
- Linux基础篇之基础操作
- CC2530基础实验之串口控制LED灯
- MapReduce并行计算框架介绍
- 386. Lexicographical Numbers\387. First Unique Character in a String
- HTML注释
- PAT--1129. Recommendation System
- HANA 2.0 SPS00 SDA(Smart Data Access)连接Hadoop
- Error:Could not download artifact 'gradle-core.jar (com.android.tools.build:gradle-core:1.3.0-beta3)
- 关于逆序输出字符串~
- 如何用命令 编译hadoop的java文件
- LeetCode:162. Find Peak Element
- 380. Insert Delete GetRandom O(1)(unsolved)
- Android之Android N 上的notification归类功能
- Nginx反向代理测试记录