首页 > 其他 > 详细

A1129 Recommendation System (25分)

时间:2020-06-09 21:43:34      阅读:37      评论:0      收藏:0      [点我收藏+]

一、技术总结

  1. 这一题,有一个没有接触到的知识点,也就是运算符重载。
  2. 这里的注意点是,每次将结果存储在set容器中,结点具备两个属性一个是值,还有一个是次数。这里重载了<号,是因为set容器里面是自动按照小于号将数值进行排序。
  3. 然后再对结果进行输出的时候,首先是将将结果输出,然后再在set容器里面查找到当前输入的结点,如果找到,那么删除,重新进行插入,将次数加一。

二、参考代码

#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;
	scanf("%d%d", &n, &k);
	set<node> s;
	for(int i = 0; i < n; i++){
		scanf("%d", &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;
} 

A1129 Recommendation System (25分)

原文:https://www.cnblogs.com/tsruixi/p/13080518.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!