SDKD 2016 Summer Single Contest #03.A

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
Input
6
1 1 1 2 2 2
Output
-1
Input
6
2 2 1 1 4 6
Output
1 2 4
1 2 6
题目:
第三次这些题,我也是醉醉的,熬夜做题,错好多次,这些天的补题一直看题解,有的是意思直接看不懂,英语可能确实不太好,有些关键词以为不重要,但是最后发现和题意有重大关系.看着满满的英语,好慌,怕读不懂,更怕读懂了不会做,有时候还要看别人的代码理解题意.
下面这个题坑了我,当时蒙了,我觉得做的很对很对的,哎~╮(╯▽╰)╭
这个题要注意细节
题目中说这些数 只能从一到七 所以 只能有以下三种可能:
124
136
126
其他的都不行,所以统计1,2,3,4,6出现的次数并满足的关系判断可不可行
很简单可以看出 [1] = [2] + [3] = [4] + [6];
但是这是不够的,不信可以试试,wa的好爽.
还有两个不等式 [2] >= [4] , [6] >= [3]
这样就可以了,然后输出,三种可能,

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 100000;
int main()
{
    int n, m;
    int a[10];
    int flag = 1;
    memset(a, 0, sizeof(a));
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &m);
        a[m]++;
        if(m == 5 || m == 7)//不可能出现5和7.
            {
                flag = 0;
                a[m] = 0;
            }
    }
    int aa = a[2] + a[3];
    int bb = a[4] + a[6];
    if(aa == a[1] && aa == bb && bb == a[1] && flag != 0 && a[2] >= a[4] && a[6] >= a[3])
    {
        while(1)
        {
            if(a[1] > 0 && a[3] > 0 && a[6] > 0)
            {
                cout << "1 3 6" << endl;
                a[1]--;
                a[3]--;
                a[6]--;
            }
            else
                break;
        }
        while(1)
        {
            if(a[1] > 0 && a[2] > 0 && a[4] > 0)
            {
                cout << "1 2 4" << endl;
                a[1]--;
                a[2]--;
                a[4]--;
            }
            else
                break;
        }
        while(1)
        {
            if(a[1] > 0 && a[2] > 0 && a[6] > 0)
            {
                cout << "1 2 6" << endl;
                a[1]--;
                a[2]--;
                a[6]--;
            }
            else
                break;
        }
    }
    for(int i = 1; i <= 7; i++)
        if(a[i] != 0)
            flag = 0;
    if(flag == 0)
        printf("-1\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值