CodeForces 342A
来源:互联网 发布:python求100以内的素数 编辑:程序博客网 时间:2024/04/29 18:40
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121074#problem/A
Description
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.
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.
Sample Input
61 1 1 2 2 2
-1
62 2 1 1 4 6
1 2 41 2 6
思路: 结果只有三种情况,
124
126
136
要输出最终结果先要判断这n个数是否能被这样分开 因为只有三种情况所以这n个数只能存在12346这五个数必须满足以下条件:
令d(x)表示x的个数
1 d(1)=n/3;
2 d(2)+d(3)=n/3;
3 d(4)+d(6)=n/3;
*4 d(2)>d(4) d(6)>d(3);
这些条件是为了保证这n个数能被分成上述的三种情况,必须是上述三种情况的充分条件条件可以适当更多更苛刻但不能少
比赛是就是没加条件4( 出错数据 6 1 1 3 3 4 4 )出错
#include<iostream>#include<iomanip>#include<cstdio>#include<cstring>#include<cmath>using namespace std;int a[7];int main(){ int n,t; while(~scanf("%d",&n)){ memset(a,0,sizeof a); for(int i=0;i<n;i++){ scanf("%d",&t); if(t==1)++a[1]; else if(t==2)++a[2]; else if(t==3)++a[3]; else if(t==4)++a[4]; else if(t==6)++a[6]; } if(a[1]!=n/3){cout<<"-1\n";continue;} else if(a[2]+a[3]!=n/3||a[4]+a[6]!=n/3){cout<<"-1\n";continue;} else if(a[4]>a[2]||a[3]>a[6]){cout<<"-1\n";continue;} for(int i=0;i<a[4];i++){ printf("1 2 4\n"); }for(int i=0;i<a[2]-a[4];i++){ printf("1 2 6\n"); }for(int i=0;i<a[3];i++){ printf("1 3 6\n"); } } return 0;}
- CodeForces 342A
- CodeForces - 342A
- CodeForces-a
- Codeforces 903A A
- CodeForces 342A Xenia and Divisors
- Codeforces 342 A. Xenia and Divisors
- CodeForces 342A Xenia and Divisors
- A. Xenia and Divisors codeforces-problem-342A
- CodeForces 18A A - Triangle
- CodeForces 133A A. HQ9+
- codeforces 651A A. Joysticks
- 【Codeforces 597A】A. Divisibility
- 【 Codeforces 615A】A. Bulbs
- A. Divisibility CodeForces 597A
- codeforces 417A A. Elimination
- CodeForces 534A A. Exam
- codeforces#98 a
- codeforces#99 a
- nyoj 243 交换输出
- Alyona and Numbers
- 【Mybatis框架】输出映射-resultType与resultMap
- 值得推荐的C/C++框架和库 (真的很强大) (转)
- iOS 分类之UIView+Frame
- CodeForces 342A
- Game Prediction
- 第一次运行TestNG测试类时报Cannot find class in classpath
- LeetCode - 337. House Robber III
- java命名规范
- 大话设计模式c++实现---建造者模式
- 超级星计划
- (Android日常)从.xml中获取数组中的@drawable
- SEO优化