As meticulous Gerald sets the table and caring Alexander sends thepostcards, Sergey makes snowmen. Each showman should consist of threesnowballs: 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 arepairwise 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 determinewhat maximum number of snowmen they can make from those snowballs.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. Thenext 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 andthe small ball's radius. It is allowed to print the snowmen in any order. Ifthere are several solutions, print any of them.
Sample test(s)
input
7
1 2 3 4 5 6 7
output
2
3 2 1
6 5 4
input
3
2 2 3
output
0
思路:
去重并记录个数,用优先队列或者集合都可以
不知道为什么用优先队列做了好久都WA,理论上是没问题的,后来上网搜搜不到用优先队列的做法的,只好参考别人用集合的代码了,不过学到了迭代器^_^
这道题是熟悉STL容器的好题
参考程序:https://snipt.net/KayvanMazaheri/cf-140c-new-year-snowmen/
程序:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define L(u) (u<<1)
#define R(u) (u<<1|1)
#define lowbit(x) (x&-x)
#define rep(i,x,y) for (i=x;i<=y;i++)
#define ll __int64
#define max(x,y) ((x>y)?(x),(y))
#define min(x,y) ((x<y)?(x),(y))
#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define slld(x) scanf("%lld",&x)
const int N = 100005;
struct node
{
int x;
int num;
friend bool operator <(node a, node b)
{
return a.x<b.x;
}
};
map<int, int> m;
struct CMP
{
bool operator()(const int &a, const int &b)const
{
if (m[a] == m[b])
return a>b;
else
return m[a]>m[b];
}
};
typedef set<int, CMP>::iterator ITR;
set<int, CMP> q;
vector<int> ans;
int main()
{
int n, i;
sd(n);
rep(i, 0, n - 1)
{
int temp;
sd(temp);
q.erase(temp);
m[temp]++;
q.insert(temp);
}
while (q.size()>2)
{
int a[3];
rep(i, 0, 2)
{
ITR it = q.begin();
a[i] = *it;
q.erase(it);
m[*it]--;
}
rep(i, 0, 2)
if (m[a[i]]>0)
q.insert(a[i]);
sort(a, a + 3);
ans.push_back(a[2]);
ans.push_back(a[1]);
ans.push_back(a[0]);
}
printf("%d\n", ans.size() / 3);
for (i = 0; i<ans.size(); i += 3)
printf("%d %d %d\n", ans[i], ans[i + 1], ans[i + 2]);
return 0;
}