1. 程式人生 > 其它 >PAT (Advanced Level) Practice 1129 Recommendation System (25 分) 凌宸1642

PAT (Advanced Level) Practice 1129 Recommendation System (25 分) 凌宸1642

PAT (Advanced Level) Practice 1129 Recommendation System (25 分) 凌宸1642

題目描述:

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.

譯:每個輸入檔案包含一個測試用例。 對於每個測試用例,第一行包含兩個正整數:N (≤ 50,000),查詢總數和 K (≤ 10),系統必須向用戶顯示的最大推薦數。 然後在第二行給出使用者正在訪問的專案的索引——為了簡單起見,所有專案的索引從 1 到 N。一行中的所有數字都用空格分隔。


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.

譯:對於每種情況,逐一處理查詢。 以以下格式在一行中輸出每個查詢的建議:

query: rec[1] rec[2] ... rec[K]

其中 query 是使用者正在訪問的專案,rec[i] (i=1, ... K) 是系統向用戶推薦的第 i 個專案。 最常訪問的前 K 個專案應該按照其頻率的非遞增順序進行推薦。 如果有平局,則專案將按其索引升序排列。

注意:第一項沒有輸出,因為當時無法給出任何建議。 保證至少有一個查詢的輸出。


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

The Idea:

  • 題目意思很明顯,我們需要給已經訪問過得資料一個計數器,每次推薦計數器更大的,如果計數器的值一樣,推薦 value值更小的
  • 可以設計一個結構體 node ,儲存每個 node 出現的次數以及 value 值,然後將其用 set 容器儲存,過載 結構體 node 的 < 運算子。

The Codes:

#include<bits/stdc++.h>
using namespace std ;
int book[50001];
struct node {
	int value, cnt;
	bool operator < (const node &a) const {
		return (cnt != a.cnt) ? cnt > a.cnt : value < a.value;
	}
};
int main() {
	int n, k, num;
	cin >> n >> k ;
	set<node> s;
	for (int i = 0; i < n; i++) {
		cin >> num ;
		if (i != 0) {
			cout << num << ":";
			int tempCnt = 0;
			for(auto it = s.begin(); tempCnt < k && it != s.end(); it++) {
				cout << ' ' << it->value ;
				tempCnt++;
			}
			cout << endl ;
		}
		auto it = s.find(node{num, book[num]});
		if (it != s.end()) s.erase(it);
		book[num]++;
		s.insert(node{num, book[num]});
	}
	return 0 ;
}