zoukankan      html  css  js  c++  java
  • Day3-E-New Year Snowmen CodeForces140C

    As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.

    Input

    The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.

    Output

    Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.

    Examples

    Input
    7
    1 2 3 4 5 6 7
    Output
    2
    3 2 1
    6 5 4
    Input
    3
    2 2 3
    Output
    0

    思路:贪心,每次选出还剩的最大的三个组成一个雪人,用优先队列为数据结构,代码如下:
    struct Node {
        int id, sum;
        Node(int _id = 0, int _sum = 0) : id(_id), sum(_sum){}
        bool operator<(const Node &a) const {
            return sum < a.sum;
        }
    };
    
    struct Snowman {
        int a, b, c;
        Snowman(int _a, int _b, int _c) : a(_a), b(_b), c(_c){}
    };
    
    bool comp(int a,int b) {
        return a > b;
    }
    map<int, int> Cache;
    vector<Snowman> res;
    int n;
    
    int main() {
        scanf("%d", &n);
        priority_queue<Node> q;
        for (int i = 0; i < n; ++i) {
            int tmp;
            scanf("%d", &tmp);
            Cache[tmp]++;
        }
        for(auto i = Cache.begin(); i != Cache.end(); ++i)
            q.push(Node(i->first,i->second));
        while(q.size() > 2) {
            Node t1, t2, t3;
            t1 = q.top(), q.pop();
            t2 = q.top(), q.pop();
            t3 = q.top(), q.pop();
            res.push_back(Snowman(t1.id, t2.id, t3.id));
            t1.sum--, t2.sum--, t3.sum--;
            if(t1.sum > 0) q.push(t1);
            if(t2.sum > 0) q.push(t2);
            if(t3.sum > 0) q.push(t3);
        }
        printf("%d
    ", res.size());
        for (int i = 0; i < res.size(); ++i) {
            int tmp[3];
            tmp[0] = res[i].a, tmp[1] = res[i].b, tmp[2] = res[i].c;
            sort(tmp, tmp + 3, comp);
            for(int j = 0; j < 3; ++j) {
                if(j)
                    printf(" ");
                printf("%d", tmp[j]);
            }
            printf("
    ");
        }
        return 0;
    }
    View Code
     
  • 相关阅读:
    洛谷 P1781 宇宙总统
    洛谷 P2524 Uim的情人节礼物·其之弐(康拓展开)
    洛谷 P1123 取数游戏
    洛谷 P4147 玉蟾宫 & P1169 [ZJOI2007]棋盘制作(求最大子矩阵)
    洛谷 P1387 最大正方形 & P2701 [USACO5.3]巨大的牛棚Big Barn (求最大子正方形)
    洛谷 P1464 Function
    洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes
    洛谷 P1160 队列安排
    洛谷 P1451 求细胞数量
    洛谷 P1914 小书童——密码
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/11239776.html
Copyright © 2011-2022 走看看