CodeForces 140C - New Year Snowmen

本文探讨如何通过合理选择不同大小的雪球来最大化制作雪人的数量,涉及从输入的雪球半径集合中找出三个不同半径的雪球进行组合。通过去重和排序等步骤,实现对输入数据的有效利用,最终输出能够构建的最大雪人数目及其具体组成。此过程展示了在有限资源条件下进行有效组合优化的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

As meticulous Gerald sets the table and caring Alexander sends thepostcards, Sergey makes snowmen. Each showman should consist of threesnowballs: a big one, a medium one and a small one. Sergey's twins help him:they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii arepairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determinewhat maximum number of snowmen they can make from those snowballs.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. Thenext line contains n integers — the balls' radii r1,r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.

Output

Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions.The description of each snowman should consist of three space-separated numbers— the big ball's radius, the medium ball's radius andthe small ball's radius. It is allowed to print the snowmen in any order. Ifthere are several solutions, print any of them.

Sample test(s)

input

7
1 2 3 4 5 6 7

output

2
3 2 1
6 5 4

input

3
2 2 3

output

0

 

思路:

去重并记录个数,用优先队列或者集合都可以

不知道为什么用优先队列做了好久都WA,理论上是没问题的,后来上网搜搜不到用优先队列的做法的,只好参考别人用集合的代码了,不过学到了迭代器^_^

这道题是熟悉STL容器的好题


参考程序:https://snipt.net/KayvanMazaheri/cf-140c-new-year-snowmen/
程序:
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
using namespace std;

#define L(u) (u<<1)
#define R(u) (u<<1|1)
#define lowbit(x) (x&-x)
#define rep(i,x,y) for (i=x;i<=y;i++)
#define ll __int64
#define max(x,y) ((x>y)?(x),(y))
#define min(x,y) ((x<y)?(x),(y))
#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define slld(x) scanf("%lld",&x)

const int N = 100005;

struct node
{
	int x;
	int num;
	friend bool operator <(node a, node b)
	{
		return a.x<b.x;
	}
};

map<int, int> m;
struct CMP
{
	bool operator()(const int &a, const int &b)const
	{
		if (m[a] == m[b])
			return a>b;
		else
			return m[a]>m[b];
	}
};

typedef set<int, CMP>::iterator ITR;
set<int, CMP> q;
vector<int> ans;

int main()
{
	int n, i;
	sd(n);

	rep(i, 0, n - 1)
	{
		int temp;
		sd(temp);

		q.erase(temp);
		m[temp]++;
		q.insert(temp);
	}

	while (q.size()>2)
	{
		int a[3];
		rep(i, 0, 2)
		{
			ITR it = q.begin();
			a[i] = *it;

			q.erase(it);
			m[*it]--;
		}

		rep(i, 0, 2)
			if (m[a[i]]>0)
				q.insert(a[i]);

		sort(a, a + 3);

		ans.push_back(a[2]);
		ans.push_back(a[1]);
		ans.push_back(a[0]);
	}

	printf("%d\n", ans.size() / 3);
	for (i = 0; i<ans.size(); i += 3)
		printf("%d %d %d\n", ans[i], ans[i + 1], ans[i + 2]);

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值