CodeForces 342A

来源:互联网 发布:python求100以内的素数 编辑:程序博客网 时间:2024/04/29 18:40

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121074#problem/A

A - Xenia and Divisors
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 342A

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 bb 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

Input
61 1 1 2 2 2
Output
-1
Input
62 2 1 1 4 6
Output
1 2 41 2 6


题意: n个正整数数(n一定是3的倍数)(每个数不超过7)3个一组分成n/3组,每组三个数分别是a,b,c. abc满足以下条件: a<b<c ,a整除b ,b整除c;

思路: 结果只有三种情况, 

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;}



0 0
原创粉丝点击