New Year Snowmen CodeForces - 140C

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

71 2 3 4 5 6 7

Output

23 2 16 5 4

Input

32 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 }

原文地址:https://www.cnblogs.com/zgglj-com/p/8495959.html

时间: 2024-10-13 06:52:56

New Year Snowmen CodeForces - 140C的相关文章

CodeForces 140C New Year Snowmen(堆)

题面 CodeForces 题解 因为要保证两两不同,所以不能单纯的开堆来维护,堆维护一个二元组,个数为第一关键字,编号为第二关键字,对于一个相同的颜色,统计一下这个颜色的个数再用堆来维护就好了. #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using std::min; using std::max; using std::sort; using st

CodeForces 140C - New Year Snowmen(数据结构)

题目链接:click here~~ [题目大意]给你一个整数序列,求最多选出每个长度为3的且序列元素单调的子序列的个数,并且输出每个子序列的元素,作为一个子序列,每个元素只能选一次,也就是满足一次性,但每个子序列里可以存在相同的元素, [解题思路]刚开始以为比较简单,就顺着思路写了一遍,第一发W了之后发现此题还是有一定的思维性,之后一直纠结在最多能选出多少子序列,因为考虑到如果序列里相同的元素的个数对最后结果会产生不同的影响,于是就想到了set容器的自动去重. 思想: 1,在判断set.size

CodeForces 140C New Year Snowm

题目链接:http://codeforces.com/contest/140/problem/C 题目大意: 给定n个数组成的序列(有重复),从中选3个数为一组,使得这三个数严格递增,请问最多能选出多少组,把每组数据输出. 分析: 很明显是贪心,不过贪心策略有待斟酌. 一开始我想当然的把数据按大小排序后从小到大贪心,结果就Wa了,很容易找到反例:1 2 3 4 4 4 5 5 5 如果从小到大贪,那么答案为1,不过这组数据眼睛看看答案都应该是3.造成这种情况的原因是我把数量少的先贪掉了,很多数量

CodeForces #100 C 贪心+STL

题目链接:CodeForces #100  C 题意:现在给出n个snowball的半径,3个半径严格递增或递减的snowball,可以组成1个snowmen.问最多能组成多少个snowmen.并且按照半径递减的顺序输出每个snowmen的组成. 思路:嗯...每次都从前三个个数最多的snowball里选择,最后组成的snowmen最多... ...可以用优先队列写..但是感觉set+map写的太优雅了...map当然不等于数组了...哼. #include <stdio.h> #includ

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store