Xenia and Divisors
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

数据最大才7。。。因此只有三种情况:1 2 4,1 2 6和1 3 6.

 #include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std; int n, cnt[], ans[]; bool deal(int v)
{
bool ok = true;
switch(v){
case :
case :
ok = !cnt[v];
break;
case :
if(cnt[] > cnt[] || cnt[] > cnt[]) {
ok = false;
break;
}
cnt[] -= cnt[];
cnt[] -= cnt[];
ans[] = cnt[];
break;
case :
if(cnt[] != cnt[] + cnt[] || cnt[] != cnt[]){
ok = false;
break;
}
ans[] = cnt[];
ans[] = cnt[];
break;
}
return ok;
} int main()
{
while(scanf("%d", &n) != EOF){
memset(cnt, , sizeof(cnt));
while(n--){
int v;
scanf("%d", &v);
cnt[v]++;
}
bool ok;
ok = deal() && deal() && deal() && deal();
if(ok){
while(ans[]--){
puts("1 2 4");
}
while(ans[]--){
puts("1 2 6");
}
while(ans[]--){
puts("1 3 6");
}
}
else{
puts("-1");
}
}
return ;
}

Xenia and Divisors的更多相关文章

  1. codeforces A. Xenia and Divisors 解题报告

    题目链接:http://codeforces.com/problemset/problem/342/A 题目意思:给出n个数,找出n/3个组且每组有3个数,这三个数必须要符合两个条件:1.a < ...

  2. Codeforces Round #199 (Div. 2) A Xenia and Divisors

    注意题目的数字最大是7 而能整除的只有 1,2,3,4,6,故构成的组合只能是1,2,4 或1,2,6或1,3,6,故分别统计1,2,3,4,6的个数,然后再分配 #include <iostr ...

  3. CF A. Xenia and Divisors

    题目大意: n(为三的倍数)个数的一个序列(每个数均不大于7),找出a,b,c a能被b整除,b能被c整除,序列中的每个数都被用到. 1 2 3 4 5 6 7 只有 1 2 4 1 2 6 1 3 ...

  4. CodeForces 342A Xenia and Divisors (水题)

    题意:给定 n 个数(小于等于7),让你把它分成 m 组,每组有三个数,且满足,a < b < c,并且 a 能整除 b,b 能整除 c. 析:对于这个题,因为题目说了是不大于7的,那么一 ...

  5. 【Codeforces 342A】Xenia and Divisors

    [链接] 我是链接,点我呀:) [题意] [题解] 最后a,b,c只有以下3种情况 1,2,4 1,2,6 1,3,6 那么用cnt[8]统计每个数字出现的次数. 输出cnt[4]次1,2,4 (如果 ...

  6. CodeForces Round 199 Div2

    完了,这次做扯了,做的时候有点发烧,居然只做出来一道题,差点被绿. My submissions     # When Who Problem Lang Verdict Time Memory 443 ...

  7. Codeforces Round #199 (Div. 2)

    A.Xenia and Divisors 题意:给定N个数,每个数的取值范围为1-7,N是3的倍数,判定是否能够恰好将N个数分成若干三元组,使得一个组中的元素a,b,c满足 a < b < ...

  8. CodeForces Round

    CodeForces Round 199 Div2   完了,这次做扯了,做的时候有点发烧,居然只做出来一道题,差点被绿. My submissions     # When Who Problem ...

  9. codeforces 27E Number With The Given Amount Of Divisors

    E. Number With The Given Amount Of Divisors time limit per test 2 seconds memory limit per test 256 ...

随机推荐

  1. [Python] 中文编码问题:raw_input输入、文件读取、变量比较等str、unicode、utf-8转换问题

    最近研究搜索引擎.知识图谱和Python爬虫比较多,中文乱码问题再次浮现于眼前.虽然市面上讲述中文编码问题的文章数不胜数,同时以前我也讲述过PHP处理数据库服务器中文乱码问题,但是此处还是准备简单做下 ...

  2. SSDB安装配置

    1.下载ssdb wget -O ssdb-master.zip https://codeload.github.com/ideawu/ssdb/zip/master 2.编译安装 unzip ssd ...

  3. windows下安装mingw

    windows环境下使用gcc MinGw是Minimal GNU on Windows的缩写,允许在GNU/linux和windows平台生成本地的windows程序而不需要第三方运行时库.本文主要 ...

  4. (笔记)VC6插件安装(VC6LineNumberAddin)

    VC6插件安装步骤如下: 1.下载VC6LineNumberAddin插件及注册文件.(以下是参考链接:) http://files.cnblogs.com/files/tdyizhen1314/VC ...

  5. iOS -iPhone5、iPhone5s、iPhone6、iPhone6Plus 屏幕适配

    现在由于苹果公司出了6和6Plus,让写苹果程序的哥们为了做兼容很头疼.用StoryBoard固然方便,但是后期做兼容要花费太多的时间和精力.使用AutoLayout虽然会在不同尺寸的屏幕下自动布局, ...

  6. Leetcode 58 Length of Last Word 字符串

    找出最后一个词的长度 class Solution { public: int lengthOfLastWord(string s) { , l = , b = ; while((b = s.find ...

  7. Android MonoGame坑记

    1.Content 加载声音API错误: 2.TouchPanel.GetState()属于抢占式的,用一次后面不能继续使用,否则状态尚属第一次: 3.TouchPanel 坐标来自于硬件本身: 4. ...

  8. Java Socket网络编程常见异常(转)

    1.java.net.SocketTimeoutException 这个异常比较常见,socket超时.一般有2个地方会抛出这个,一个是connect的时候,这个超时参数由connect(Socket ...

  9. 巧用Windows 7计划任务设置定时提醒

    Windows 7系统有个“计划任务”功能,一般人都很少使用.其实,“计划任务”是系统自带的一个很实用的功能,比如说,这个功能可以设置定时提醒,这样在使用电脑时就不会因为太过投入而导致错过重要的事务. ...

  10. Sencha Toucha 2 —1.环境安装配置、在线打包、离线打包

    环境安装配置        1. 下载 1.1     Sencha Touch 下载 http://cdn.sencha.com/touch/sencha-touch-2.2.1-gpl.zip 1 ...