Codeforces 342 A. Xenia and Divisors

简介:
A. Xenia and Divisors
time limit per test
1 second
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.

Sample test(s)
input
6
1 1 1 2 2 2
output
-1
input
6
2 2 1 1 4 6
output
1 2 4
1 2 6
题目大意:
给你一个数m,然后有m个数arr[i],每个数都<=7 && >=1,让你找满足以下关系的数(3个一组3个一组)
1)a<b<c;
2)a能被b整除,b能被c整除;
解题思路:
首先想到满足关系的三个数
1 2 4
1 2 6
1 3 6
然后再找不属于这三个数的条件,只要去掉不满足条件的就ok了,
不满足条件的:
1)1 的个数必须是m/3;
2)不能有5和7;
3)2 的个数必须大于等于4的个数
4) 6 的个数必须大于等于3的个数
5)2 的个数 + 3的个数  == 6的个数 + 4的个数
然后上代码:
/**
Author: ITAK

Date: 2015-09-29
**/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;

#define MM(a) memset(a,0,sizeof(a))

typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 100000+5;
const int mod = 1000000007;
const double eps = 1e-7;

int cmp(int a, int b)
{
    return a > b;
}
int gcd(int a, int b)
{
    if(b == 0)
        return a;
    else
        return gcd(b, a%b);
}
int arr[maxn];
int data[10];
int main()
{
    int m;
    scanf("%d",&m);
    MM(data);
    for(int i=0; i<m; i++)
    {
        scanf("%d",&arr[i]);
        data[arr[i]]++;
    }
    m /= 3;
    if(data[1]!=m || data[5] | data[7] || data[2]<data[4] ||
       data[6]<data[3] || data[2]+data[3]!=data[4]+data[6])
        puts("-1");
    else
    {
        for(int i=1; i<=data[4]; i++)
            puts("1 2 4");
        for(int i=1; i<= data[2]-data[4]; i++)
            puts("1 2 6");
        for(int i=1; i<=data[3]; i++)
            puts("1 3 6");
    }
    return 0;
}


目录
相关文章
codeforces 322 B Ciel and Flowers
有红绿蓝三种颜色的画,每种拿三朵可以组成一束花,或者各拿一朵组成花束,告诉你每种花的数目,求出可能组成最多的花束。 如果你的代码过不了,考虑一下 8 8 9这种组合。 因为数据量很大,我的思想就是局部和总体采用不同的策略。
52 0
|
6月前
codeforces
【6月更文挑战第10天】
32 0
codeforces 304A. Pythagorean Theorem II
给你一个n,计算出1 ≤ a ≤ b ≤ c ≤ n.使得由abc构成的三角形满足勾股定理,c为斜边。 没有简单的方法,直接爆力,但是要注意,有些abc满足勾股定理的表达式,但不一定是三角形,所以要判断一下,根据三角形三边的性质,两边之和大于第三边,两边之差小于第三边。
66 0
C - Rumor CodeForces - 893C
C - Rumor CodeForces - 893C
92 0
|
数据安全/隐私保护
Codeforces 417D.Cunning Gena (状压DP)
Codeforces 417D.Cunning Gena (状压DP)
88 0
|
人工智能
Codeforces 777C Alyona and Spreadsheet
C. Alyona and Spreadsheet time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard ...
1160 0
|
人工智能
Codeforces 719B Anatoly and Cockroaches
B. Anatoly and Cockroaches time limit per test:1 second memory limit per test:256 megabytes input:standard input output:stan...
894 0