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 ...
随机推荐
- lucene字典实现原理
http://www.cnblogs.com/LBSer/p/4119841.html 1 lucene字典 使用lucene进行查询不可避免都会使用到其提供的字典功能,即根据给定的term找到该te ...
- Java攻城狮面试考题
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- hdu4508 完全背包,湫湫系列故事——减肥记I
湫湫系列故事——减肥记I 对于01背包和完全背包,昨晚快睡着的时候,突然就来了灵感 区别:dp[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值 在第二重循环,01 是倒着循环的,因 ...
- python中argparse模块的使用
有两个文件一个是 文件1:sync_shop_source_bimer.sh 文件2:sync_shop_source_bimer.py 在sync_shop_source_bimer.sh 中调用s ...
- file_get_contents高級用法
首先解決file_get_contents的超時問題,在超時返回錯誤後就象js中的settimeout那樣進行一次嘗試,錯誤超過3次或者5次後就確認為無法連線伺服器而徹底放棄.這裡就簡單介紹兩種解決方 ...
- 只写104行代码!在nopCommerce中如何实现自动生成网站地图
表告诉我说你不知道nopCommerce是什么.它是目前.NET中最流行的完全开源网上商城,由俄罗斯的团队在2008年开始立项一直开发到现在已经是3.3版本了.代码目前托管在codeplex上,有兴趣 ...
- Postman测试Web API
如何查看查询字符串? →输入包含查询字符串的URL,比如:http://localhost:54176/api/ProductCategories?name=darren&age=25→点击P ...
- 分享一些无特征PHP一句话
分享些不需要动态函数.不用eval.不含敏感函数.免杀免拦截的一句话.(少部分一句话需要php5.4.8+.或sqlite/pdo/yaml/memcached扩展等) 原理:https://www. ...
- 《STL系列》之map原理及实现
上一篇文章<STL系列>之vector原理及实现,介绍了vector的原理及实现,这篇文章介绍map的原理及实现.STL实现源码下载.STL中map的实现是基于RBTree的,我在实现的时 ...
- 2014 Hangjs 见闻流水账第二天
前言 第一天传送门: 2014 Hangjs 见闻流水账第一天 写作风格跟第一天还是一样的. Slide 每个slide我都会根据自己的理解重新命名一次,用于表达自己的第一看法,主观意见,不喜可吐槽, ...