PAT甲级——1129 Recommendation System (25 分)

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:

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

题目大意: 依次输入N个数,从第二个数开始,每输入一个数字,对应输出按照既定规则排序后的推荐序列;该序列的元素个数≤K,元素为之前读取的数字,排序规则为:按数字出现的次数递减,次数相同时按数字本身大小递增。

思路:用一个结构体node存储数字的值vertex和出现的次数time,建立一个大小为K的node数组rec,然后排序,排序非常简单粗暴,调用库函数sort()即可。排序前需要将情况分类,元素种类少于K的时候直接对数组排序;元素种类大于K的时候,先判断新元素是否在rec里面,若不在rec里,则将rec的末尾元素与新元素比较来决定要不要用新元素覆盖末尾元素;若在rec里,则将rec里对应的元素的出现次数+1;最后对rec重新排序输出。

/*以下代码在OJ平台AC了,但是dev c++可能需要稍作修改*/
#include <iostream>
#include<unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
struct node {
	int vertex,time=0;
};
vector<node> rec;
unordered_map<int, node> mp;
unordered_map<int, bool> exist;
bool cmp(node a,node b) {
	if (a.time != b.time)
		return a.time > b.time;
	else
		return a.vertex < b.vertex;
}
int main()
{
	int N, K, vertex;
	scanf("%d%d%d", &N, &K, &vertex);
	rec.resize(K);
	for (int i = 2; i <= N; i++) {
		int next;
		scanf("%d", &next);
		mp[vertex].vertex = vertex;
		mp[vertex].time++;
		int m = mp.size();
		if (m <= K) {
			int cnt=0;
			for (auto it = mp.begin(); it != mp.end(); it++) {
				rec[cnt] = { it->second.vertex,it->second.time };//dev c++此处可能会报错,但visual studio和OJ平台都没问题
				exist[it->first] = true;
				cnt++;
			}
			sort(rec.begin(), rec.begin() + cnt, cmp);
			printf("%d:", next);
			for (int j = 0; j < cnt; j++)
				printf(" %d", rec[j]);
			printf("\n");
		}
		else {
			if (!exist[vertex]) {
				node tmp = { vertex, mp[vertex].time };
				if (cmp(tmp, rec[K - 1])) {
					exist[rec[K - 1].vertex] = false;
					rec[K - 1] = tmp;
					exist[vertex] = true;
				}
			}
			else {
				for (int j = 0; j < K; j++)
					if (rec[j].vertex == vertex) 
						rec[j].time = mp[vertex].time;
			}
			sort(rec.begin(), rec.end(), cmp);
			printf("%d:", next);
			for (int j = 0; j < K; j++)
				printf(" %d", rec[j]);
			printf("\n");
		}
		vertex = next;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_44385565/article/details/89042135