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;
}
时间: 2024-10-01 02:19:12

Codeforces 342 A. Xenia and Divisors的相关文章

算法题:CodeForces 236B

链接: http://www.codeforces.com/problemset/problem/236/B 题目: B. Easy Number Challenge time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Let's denote d(n) as the number of divisors of a positiv

Codeforces Round #157 (Div. 1) C. Little Elephant and LCM (数学、dp)

C. Little Elephant and LCM time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output The Little Elephant loves the LCM (least common multiple) operation of a non-empty set of positive integers. The

Codeforces B. Taxi 算法题解

简单总结题意: 有一个数组,数组中的数值不会大于4,即只能是1,2,3,4,要把这些数值装进一个容量最大为4的容器里面,使得所用到这样的容器的个数最小. 经测试数据很大,会有10万个数据,所以这里我并不用排序数据,而是使用counting sort的思想,根据特定的数据优化,使得题解时间复杂度为O(n). 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/ 程序如下: #include <iostream>

CodeForces 233B Non-square Equation

链接: http://codeforces.com/problemset/problem/233/B 题目: B. Non-square Equation time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Let's consider equation: x2+s(x)·x-n=0, where x,n are positive

CodeForces:200C: Football Championship

地址链接: CF:  http://codeforces.com/problemset/problem/200/C HUST Virtual Judge: http://acm.hust.edu.cn:8080/judge/problem/viewProblem.action?id=28923 题目: C. Football Championship time limit per test 2 seconds memory limit per test 256 megabytes input s

Codeforces Round #205 (Div. 2) / 353C Find Maximum (贪心)

Valera has array a, consisting of n integers a0,a1,...,an-1, and function f(x), taking an integer from 0 to 2n-1 as its single argument. Value f(x) is calculated by formula , where value bit(i) equals one if the binary representation of number xconta

Codeforces Round #201 (Div. 1) / 346A Alice and Bob:数论&amp;amp;想法题

A. Alice and Bob http://codeforces.com/problemset/problem/346/A time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output It is so boring in the summer holiday, isn't it? So Alice and Bob have inven

算法:CodeForces #196(Div. 2) 337D Book of Evil(树形dp)

题意 给一棵n个结点的树,任意两个节点的距离是指连接两点的最短的边数 在树上的某个结点 有一个"恶魔之书",这本书会让距离它d以内的节点都受到影响 已知有m个节点收到了影响,问最多有几 个结点可能放着"恶魔之书"? 思路 要判断某个点是不是放着书,就要判断这个点的周围d距离以 内是否包含所有受影响的m节点 而如果某个节点距离最远的那个受影响节点的距离是L,如果L <= d,那 么说明所有受影响的m节点都在d以内,就可判断这个点可能放着书 那么,我们只要能够求出

Codeforces Round #100 / 140A:简单几何

http://codeforces.com/contest/140/problem/A 过大圆圆心作小圆切线即可发现规律,详见代码. 注意判相等一定要用fabs!!! 完整代码: /*30ms,0KB*/ #include<bits/stdc++.h> using namespace std; int main() { int n, R, r; double a; scanf("%d%d%d", &n, &R, &r); if (r > R |