A1129 Recommendation System [set自定义排序]

在这里插入图片描述

#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<set>
#include<queue>
#include<unordered_set>
using namespace std;
using namespace std;
int book[50001];
struct node {
	int value, cnt;
	node(int a, int b) :value(a), cnt(b) {}
	bool friend operator < (node a,node b)
	{
		if (a.cnt != b.cnt)
			return a.cnt > b.cnt;
		else
			return a.value < b.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) {
			printf("%d:", num);
			int tempCnt = 0;
			for (auto it = s.begin(); tempCnt < k && it != s.end(); it++) {
				printf(" %d", it->value);
				tempCnt++;
			}
			printf("\n");
		}
		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;
}
posted @ 2020-08-12 11:17  _Hsiung  阅读(44)  评论(0编辑  收藏  举报