CF490A Team Olympiad 题解

Content

\(n\) 个孩子参加比赛,每个孩子有一种属性值 \(a_i\),保证其为 \(1\)\(3\) 之内的整数。已知每个队伍要三个孩子,每个孩子的 \(a_i\) 都不能相同,询问能够组出多少组队伍,并且输出任意一组方案(如果答案是 \(0\) 则不输出方案)。

数据范围:\(n\leqslant 10^5\)(个人推测),\(a_i\) 如上。

附注:\(n\) 的具体范围在题面和翻译中均未知,但根据题解里面开的数据大小,可以肯定的是 \(n\leqslant 10^5\)

Solution

首先显然可以确定的是:设答案为 \(ans\),如果属性值为 \(a_i\) 的孩子有 \(num_{a_i}\) 个,那么 \(ans\) 就是三个 \(num\) 的最小值。 具体原因很显然,类似于一个木桶效应。

啥啥啥,你没听说过木桶效应?

自行百度吧。\(\rightarrow\text{Link}\)

输出的时候直接按照编号为 \(1\)\(ans\) 输出就好了,实在不行的话可以参照下代码理解。

Code

#include <cstdio>
#include <algorithm>
using namespace std;

int n, a[1000007], cplusplus[1000007], math[1000007], pe[1000007], cnt1, cnt2, cnt3, ans;

int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
		if(a[i] == 1)	cplusplus[++cnt1] = i;
		if(a[i] == 2)	math[++cnt2] = i;
		if(a[i] == 3)	pe[++cnt3] = i;
	}
	ans = min(cnt1, min(cnt2, cnt3));
	printf("%d\n", ans);
	for(int i = 1; i <= ans; ++i) {
		printf("%d %d %d\n", cplusplus[i], math[i], pe[i]);
	}
	return 0;
}
posted @ 2021-12-23 20:19  Eason_AC  阅读(32)  评论(0)    收藏  举报