Xenia and Divisors
2 seconds
256 megabytes
standard input
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 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.
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.
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的更多相关文章
- codeforces A. Xenia and Divisors 解题报告
题目链接:http://codeforces.com/problemset/problem/342/A 题目意思:给出n个数,找出n/3个组且每组有3个数,这三个数必须要符合两个条件:1.a < ...
- 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 ...
- 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 ...
- CodeForces 342A Xenia and Divisors (水题)
题意:给定 n 个数(小于等于7),让你把它分成 m 组,每组有三个数,且满足,a < b < c,并且 a 能整除 b,b 能整除 c. 析:对于这个题,因为题目说了是不大于7的,那么一 ...
- 【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 (如果 ...
- CodeForces Round 199 Div2
完了,这次做扯了,做的时候有点发烧,居然只做出来一道题,差点被绿. My submissions # When Who Problem Lang Verdict Time Memory 443 ...
- Codeforces Round #199 (Div. 2)
A.Xenia and Divisors 题意:给定N个数,每个数的取值范围为1-7,N是3的倍数,判定是否能够恰好将N个数分成若干三元组,使得一个组中的元素a,b,c满足 a < b < ...
- CodeForces Round
CodeForces Round 199 Div2 完了,这次做扯了,做的时候有点发烧,居然只做出来一道题,差点被绿. My submissions # When Who Problem ...
- 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 ...
随机推荐
- cookie入门与学习
据我对cookie诞生背景的了解,cookie是由网景公司创建的,目的就是将用户的数据储存在客户端上.伴随的HTML5的出现,现在又有另外一个解决数据离线储存的方案,就是HTML5中的Web stor ...
- iOS开发-友盟分享使用(2)
1.友盟SDK提供功能:分享喜欢的东西到新浪微博.qq空间.为微信朋友圈等等等等社交圈. 2.友盟分享前期准备 (1)注册账号 去官网 (2)创建应用获取appkey 类似5556a53667e*** ...
- JAVA学习Swing章节JPanel和JScrollPane面板的简单学习
package com.swing; import java.awt.Container; import java.awt.GridLayout; import javax.swing.JButton ...
- Shell脚本学习入门(一)
1.Shell脚本是解释型的语言. 2.Shell脚本建立的基本步骤: 3.Shell脚本文件的第一行一般可以是:"#! 路径名 -(选项)", 为了要使Shell脚本有移植性,请 ...
- C# winform的WebBrowser非常规编程(强烈推荐)
本文章被今日头条推荐 1.在WebBrowser中实现抓取301和302协议 在WebBrowser中抓取301和302协议目前官方提供的组件远远不够,需要借助HttpMonitor.dll.这个组件 ...
- python多线程生成缩略图
在img目录下7张图片 分别是 11.jpg 22.jpg 33.jpg 44.jpg 55.jpg 66.jpg 77.jpg #encoding=utf-8 import os import ti ...
- python find函数
Python find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1 ...
- 深入理解.NET程序的原理 谈一谈破解.NET软件的工具和方法
最近一段时间不忙,闲下来的空闲时间,重读了一下CLR的原理,回味一下有关程序集的的知识,顺便练了一下手,学习致用,破解了若干个.NET平台的软件.以此来反观.NET程序开发中,需要注意的一些问题. 基 ...
- 【VerySky原创】 ME9F
[VerySky原创] V_EKKONA - Generierte Tabelle zu einem View V_EKKONA-PFLD4 字段 SE71查看 至于图片:在表STXBITMAPS中 ...
- 原创内容搬家到csdn博客啦~
以后原创的文章就发布在csdn博客啦: http://blog.csdn.net/aceyan0718 这里就用来当作一个网络笔记本吧,转载些优质的内容