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 ...
随机推荐
- 【转】关于Mahalanobis距离的笔记
Mahalanobis距离是用来度量一个点P和一个分布D之间的距离,它是衡量点P与分布D的均值之间存在多少个标准差的一个多维泛化版本. 如果P就位于分布D的均值处,则该距离为0:该距离随着P的偏离均值 ...
- 【设计模式】Java版设计模式的类图汇总
Abstract Factory Intent: Provide an interface for creating families of related or dependent objects ...
- atitit.人脸识别的应用场景and使用最佳实践 java .net php
atitit.人脸识别的应用场景and使用最佳实践 java .net php 1. 人脸识别的应用场景 1 2. 框架选型 JNI2OpenCV.dll and JavaCV 1 3. Url ap ...
- Python核心编程(切片索引的更多内容)
# coding=utf8 s = 'abcde' i = -1 for i in range(-1, -len(s), -1): print(s[:i]) # 结果 ''' abcd abc ab ...
- BizTalk 2013R2 WCF-LOB Oracle Adapter安装配置/问题&解决方法
BizTalk 2013R2 WCF-LOB Oracle Adapter安装配置/问题&解决方法 安装Oracle Adapter 安装Oracle客户端 BizTalk 2013R2 安装 ...
- Git – fatal: Unable to create ‘/.git/index.lock’: File exists错误解决办法
有时候在提交的时候,中间提交出错,导致有文件被lock,所以会报下面的错误: fatal: Unable to create ‘/msg/.git/index.lock’: File exists. ...
- 64位Windows无法打开会声会影X5的解决方法
低版本和破解版都不好用了. 由于微软更新补丁而引起的会声会影停止工作的问题,建议大家卸载补丁[KB3126587]和[KB3126593][KB2140410 ]解决或者(修复包)修复,更换高版本使用 ...
- Word中怎么设置标题的自动编号
转自: http://jingyan.baidu.com/article/4f7d5712c39c0a1a201927ea.html
- Android 设置VPN(pptp连接方式)
本教程以小米手机的MIUI系统为例子,教大家如何设置VPN 先找到“设置”,打开设置菜单,如下图: 在设置菜单里面找“其它连接方式” 然后找到“VPN”,点击进入: 进入VPN设置界面后,如果VPN未 ...
- Linux--U盘安装Ubuntu12.04
前言 最近一直在研究Android内核驱动开发的相关事宜,使用VMware虚拟机虽然可以更方便的开发,但是对于开发环境硬件的要求还是比较高的,若用于开发,效率太低了,所以考虑使用单独PC去装载Linu ...