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. LINQ-to-SQL那点事~LINQ-to-SQL中的数据缓存与应对

    回到目录 这个文章写的有点滞后了,呵呵,因为总想把之前不确定的东西确定了之后,再写这篇,之前的LINQ-to-SQL那点事,请点这里. LINQ-to-SQL中的数据缓存与应对 Linq-to-SQL ...

  2. 浅入“Block Formatting Context”

    本文主要是针对BFC特性的应用,至于什么是BFC,可以参看MDN的简介: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Block_fo ...

  3. Leetcode-121 Best Time to Buy and Sell Stock

    #121   Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price ...

  4. 在线教程的游戏化-20分钟做了个demo

    首先,不准说做得撇,因为其一,我只用了20分钟不到:其二,第一次尝试,以前想过,但是一直没有搞过,二话不说,先来截图,下载地址在最下面. 因为第一次尝试,所以很多事件自己还没有闹明白,不过基本上还是看 ...

  5. 在Servlet中出现一个输出中文乱码的问题(已经解)。

    在Servlet中出现一个输出中文乱码的问题,已经解. @Override public void doPost(HttpServletRequest reqeust, HttpServletResp ...

  6. DataTable 排序

    DataTable newdtb = new DataTable(); newdtb.Columns.Add("Id", typeof(int)); newdtb.Columns. ...

  7. WPF调用图片路径,或资源图片

    一.加载本项目的图片WPF引入了统一资源标识Uri(Unified Resource Identifier)来标识和访问资源.其中较为常见的情况是用Uri加载图像.Uri表达式的一般形式为:协议+授权 ...

  8. ATA/SATA/SCSI/SAS/FC总线简介

    ATA/SATA/SCSI/SAS/FC 都是应用于存储领域的总线,在当今的存储系统中,普遍应用的硬盘接口主要有 SATA . SCSI . SAS 和FC , ATA 比较古老,在一些老的低端存储系 ...

  9. memcached命令

    memcached相对于redis来说,简直简单太多,命令也少很多,一般应用都是使用redis,但了解一下也还是不错的. 具体命令和用法很参见:http://www.runoob.com/memcac ...

  10. 让MyEclipse2013兼容Retina屏幕

    1. 找到文件:/Applications/MyEclipse/MyEclipse Professional.app/Contents/Profile/myeclipse.app/Contents/I ...