码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Team Olympiad(暴力)

时间:2014-11-26 01:29:20      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   sp   

***********************************************声明******************************************************

      原创作品,出自 “晓风残月xj” 博客,欢迎转载,转载时请务必注明出处(http://blog.csdn.net/xiaofengcanyuexj)。

      由于各种原因,可能存在诸多不足,欢迎斧正!

*********************************************************************************************************

A. Team Olympiad
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The School №0 of the capital of Berland has n children studying in it. All the children in this school are gifted: some of them are good at programming, some are good at maths, others are good at PE (Physical Education). Hence, for each child we know value ti:

  • ti?=?1, if the i-th child is good at programming,
  • ti?=?2, if the i-th child is good at maths,
  • ti?=?3, if the i-th child is good at PE

Each child happens to be good at exactly one of these three subjects.

The Team Scientific Decathlon Olympias requires teams of three students. The school teachers decided that the teams will be composed of three children that are good at different subjects. That is, each team must have one mathematician, one programmer and one sportsman. Of course, each child can be a member of no more than one team.

What is the maximum number of teams that the school will be able to present at the Olympiad? How should the teams be formed for that?

Input

The first line contains integer n (1?≤?n?≤?5000) — the number of children in the school. The second line contains n integers t1,?t2,?...,?tn (1?≤?ti?≤?3), where ti describes the skill of the i-th child.

Output

In the first line output integer w — the largest possible number of teams.

Then print w lines, containing three numbers in each line. Each triple represents the indexes of the children forming the team. You can print both the teams, and the numbers in the triplets in any order. The children are numbered from 1 to n in the order of their appearance in the input. Each child must participate in no more than one team. If there are several solutions, print any of them.

If no teams can be compiled, print the only line with value w equal to 0.

Sample test(s)
Input
7
1 3 1 3 2 1 2
Output
2
3 5 2
6 7 4
Input
4
2 1 1 2
Output
0

题解:

       本题要求把1,2,3尽可能的平均分配到3个容器中,保证每一个都包含1、2、3,求这样的最大组合数以及输出一种可能的组合。可以暴力枚举!时间复杂度O(n),空间复杂度也为O(n)。

源代码:

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

const int MAXN=5000+100;
int per[3][MAXN];

int min(int a,int b)
{
	return a<b?a:b;
}

int main()
{
	int n,cnt1,cnt2,cnt3,tmp,count,i;
	while(~scanf("%d",&n))
	{
		cnt1=0,cnt2=0,cnt3=0;
		i=1;
		while(i<=n)
		{
			scanf("%d",&tmp);
			switch(tmp)
			{
			case 1:per[0][cnt1++]=i;break;
			case 2:per[1][cnt2++]=i;break;
			case 3:per[2][cnt3++]=i;break;
			}
			++i;
		}	
		count=min(min(cnt1,cnt2),cnt3);	
		printf("%d\n",count);
		for(int i=0;i<count;i++)
		{
			printf("%d %d %d\n",per[0][i],per[1][i],per[2][i]);
		}
	}
	return 0;
}





Codeforces Team Olympiad(暴力)

标签:des   style   blog   http   io   ar   color   os   sp   

原文地址:http://blog.csdn.net/xiaofengcanyuexj/article/details/41495709

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!