zoukankan      html  css  js  c++  java
  • Xenia and Divisors

    Xenia and Divisors
    time limit per test
    2 seconds
    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.

    数据最大才7。。。因此只有三种情况:1 2 4,1 2 6和1 3 6.

     1 #include <iostream>
     2 #include <queue>
     3 #include <cstdio>
     4 #include <cstring>
     5 using namespace std;
     6 
     7 int n, cnt[8], ans[3];
     8 
     9 bool deal(int v)
    10 {
    11     bool ok = true;
    12     switch(v){
    13     case 7:
    14     case 5:
    15         ok = !cnt[v];
    16         break;
    17     case 4:
    18         if(cnt[4] > cnt[2] || cnt[4] > cnt[1]) {
    19             ok = false;
    20             break;
    21         }
    22         cnt[2] -= cnt[4];
    23         cnt[1] -= cnt[4];
    24         ans[0] = cnt[4];
    25         break;
    26     case 6:
    27         if(cnt[6] != cnt[2] + cnt[3] || cnt[6] != cnt[1]){
    28             ok = false;
    29             break;
    30         }
    31         ans[1] = cnt[2];
    32         ans[2] = cnt[3];
    33         break;
    34     }
    35     return ok;
    36 }
    37 
    38 int main()
    39 {
    40     while(scanf("%d", &n) != EOF){
    41         memset(cnt, 0, sizeof(cnt));
    42         while(n--){
    43             int v;
    44             scanf("%d", &v);
    45             cnt[v]++;
    46         }
    47         bool ok;
    48         ok = deal(7) && deal(5) && deal(4) && deal(6);
    49         if(ok){
    50             while(ans[0]--){
    51                 puts("1 2 4");
    52             }
    53             while(ans[1]--){
    54                 puts("1 2 6");
    55             }
    56             while(ans[2]--){
    57                 puts("1 3 6");
    58             }
    59         }
    60         else{
    61             puts("-1");
    62         }
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    解决Java版CKFinder无法显示缩略图问题
    python视频教程大全
    关于Linux vi命令 vi命令一览表
    Python快速教程
    Linux的概念与体系
    每天一个linux命令目录
    每天一个linux命令(31): /etc/group文件详解
    每天一个linux命令(30): chown命令
    每天一个linux命令(29):chgrp命令
    十大Material Design开源项目
  • 原文地址:https://www.cnblogs.com/cszlg/p/3307989.html
Copyright © 2011-2022 走看看