CodeForces 140C New Year Snowmen (贪心+优先队列)

题意:n个数,选三个严格下降的数为一组,求最多能选多少组,并列出每组哪些数。

题解:贪心+优先队列
最多能选多少组,那么必须贪心数量多的。
例如:1 1 2 3 4 5
如果按照数的大小排序,只能贪到1组,如果按照数量排序,那么就是2组。
这样一来,我们可以建立以数量为标准的优先队列,每次选择数量前3多的数为一组即可。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
using namespace std;
const int maxn = 1e5 + 5;
int n, r[maxn], a[5];
map<int, int> m;
priority_queue<pair<int, int> > q;
queue<int> p;
queue<pair<int, int> > pp;
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &r[i]);
		m[r[i]]++;
	}
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
		q.push(make_pair(it->second, it->first));
	}
	while (q.size() >= 3) {
		for (int i = 1; i <= 3; i++) {
			pair<int, int> temp = q.top();
			q.pop();
			temp.first--;
			if(temp.first) pp.push(temp);
			p.push(temp.second);
		}
		while (!pp.empty()) {
			q.push(pp.front());
			pp.pop();
		}
	}
	printf("%d\n", p.size() / 3);
	int num = 0;
	while (!p.empty()) {
		a[++num] = p.front();
		p.pop();
		if (num == 3) {
			sort(a + 1, a + 4);
			printf("%d %d %d\n", a[3], a[2], a[1]);
			num = 0;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值