http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121074#problem/A
Description
Xenia the mathematician has a sequence consisting of n (n is divisible by 3) positive integers, each of them is at most 7. She wants to split the sequence into groups of three so that for each group of three a, b, c the following conditions held:
- a < b < c;
- a divides b, b divides c.
Naturally, Xenia wants each element of the sequence to belong to exactly one group of three. Thus, if the required partition exists, then it has groups of three.
Help Xenia, find the required partition or else say that it doesn't exist.
Input
The first line contains integer n(3 ≤ n ≤ 99999) — the number of elements in the sequence. The next line contains n positive integers, each of them is at most 7.
It is guaranteed that n is divisible by 3.
Output
If the required partition exists, print groups of three. Print each group as values of the elements it contains. You should print values in increasing order. Separate the groups and integers in groups by whitespaces. If there are multiple solutions, you can print any of them.
If there is no solution, print -1.
Sample Input
6 1 1 1 2 2 2
-1
6 2 2 1 1 4 6
1 2 4 1 2 6
思路: 结果只有三种情况,
124
126
136
要输出最终结果先要判断这n个数是否能被这样分开 因为只有三种情况所以这n个数只能存在12346这五个数 必须满足以下条件:
令d(x)表示x的个数
1 d(1)=n/3;
2 d(2)+d(3)=n/3;
3 d(4)+d(6)=n/3;
*4 d(2)>d(4) d(6)>d(3);
这些条件是为了保证这n个数能被分成上述的三种情况,必须是上述三种情况的充分条件条件可以适当更多更苛刻但不能少
比赛是就是没加条件4( 出错数据 6 1 1 3 3 4 4 )出错
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int a[7];
int main(){
int n,t;
while(~scanf("%d",&n)){
memset(a,0,sizeof a);
for(int i=0;i<n;i++){
scanf("%d",&t);
if(t==1)++a[1];
else if(t==2)++a[2];
else if(t==3)++a[3];
else if(t==4)++a[4];
else if(t==6)++a[6];
}
if(a[1]!=n/3){cout<<"-1\n";continue;}
else if(a[2]+a[3]!=n/3||a[4]+a[6]!=n/3){cout<<"-1\n";continue;}
else if(a[4]>a[2]||a[3]>a[6]){cout<<"-1\n";continue;}
for(int i=0;i<a[4];i++){
printf("1 2 4\n");
}for(int i=0;i<a[2]-a[4];i++){
printf("1 2 6\n");
}for(int i=0;i<a[3];i++){
printf("1 3 6\n");
}
}
return 0;
}