zoukankan      html  css  js  c++  java
  • New Year Snowmen CodeForces

    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 r1, r2, ..., 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 r1, r2, ..., 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.

    Example

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

    题解:每次取数量最多的三个,记录,然后数量减一,再重新取数量最多的三个。正确性不知道怎么证明。
     1 // ConsoleApplication1.cpp: 定义控制台应用程序的入口点。
     2 //
     3 
     4 #include "stdafx.h"
     5 #include<map>
     6 #include<queue>
     7 #include<cstdio>
     8 #include<cstring>
     9 #include<iostream>
    10 #include<algorithm>
    11 using namespace std;
    12 
    13 const int maxn = 100005;
    14 
    15 int n;
    16 int vec[maxn][3];
    17 
    18 struct node {
    19     int x, num;
    20     node(int x, int num) :x(x), num(num) {};
    21     bool operator<(const node& i)const {
    22         if (num == i.num) return x < i.x;
    23         else return num < i.num;
    24     }
    25 };
    26 
    27 int main()
    28 {
    29     while (scanf_s("%d", &n) != EOF) {
    30         map<int, int> p;
    31         int temp;
    32         for (int i = 0; i < n; i++) {
    33             scanf_s("%d", &temp);
    34             p[temp]++;
    35         }
    36         priority_queue<node> q;
    37         map<int, int>::iterator it;
    38         for (it = p.begin(); it != p.end(); it++) q.push(node(it->first, it->second));
    39         int t = 0;
    40         while (!q.empty()) {
    41             if (q.size() < 3) break;
    42             node a = q.top(); q.pop();
    43             node b = q.top(); q.pop();
    44             node c = q.top(); q.pop();
    45             vec[t][0] = a.x; vec[t][1] = b.x; vec[t][2] = c.x;
    46             t++;
    47             if (a.num - 1 != 0) q.push(node(a.x, a.num - 1));
    48             if (b.num - 1 != 0) q.push(node(b.x, b.num - 1));
    49             if (c.num - 1 != 0) q.push(node(c.x, c.num - 1));
    50         }
    51         cout << t << endl;
    52         for (int i = 0; i < t; i++) {
    53             sort(vec[i], vec[i] + 3);
    54             cout << vec[i][2] << " " << vec[i][1] << " " << vec[i][0] << endl;
    55         }
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    Leetcode#117 Populating Next Right Pointers in Each Node II
    Leetcode#123 Best Time to Buy and Sell Stock III
    获取文件大小的方法
    内存映射
    git patch
    git cherry-pick
    关于extern的说明
    Linux如何查看与/dev/input目录下的event对应的设备
    如何在Linux下统计高速网络中的流量
    [: ==: unary operator expected 解决方法
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/8495959.html
Copyright © 2011-2022 走看看