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 radii1, 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 maximumnumber of snowmen they can make from those snowballs.
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.
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.
7 1 2 3 4 5 6 7
2 3 2 1 6 5 4
3 2 2 3
0
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include <iostream>
#include<algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int maxn = 100000 + 5;
struct Node{
int id,sum;
bool operator < (const Node & a) const{
return sum < a.sum;
}
};
map<int,int> M;
map<int,int>::iterator it;
vector<int> ans[maxn];
priority_queue<Node> Q;
int main(){
int n;
while(cin >> n){
M.clear();
while(!Q.empty()) Q.pop();
for(int i = 0;i < n;i++) ans[i].clear();
for(int i = 0;i < n;i++){
int tem;
cin >> tem;
M[tem]++;
}
for(it = M.begin();it != M.end();it++){
Q.push(Node{it->first,it->second});
}
int cnt = 0;
while(Q.size()>=3){
Node A[3];
A[0] = Q.top();Q.pop();
A[1] = Q.top();Q.pop();
A[2] = Q.top();Q.pop();
for(int i = 0;i < 3;i++) ans[cnt].push_back(A[i].id);
sort(ans[cnt].begin(),ans[cnt].end(),greater<int>());
cnt++;
for(int i = 0;i < 3;i++){
A[i].sum--;
if(A[i].sum > 0) Q.push(A[i]);
}
}
cout << cnt << endl;
for(int i = 0;i < cnt;i++){
for(int j = 0;j < 3;j++) cout << ans[i][j] << ' ';
cout << endl;
}
}
return 0;
}
本文介绍了一种算法问题,旨在帮助用户找出从给定不同尺寸雪球中能组成的最大数量的雪人。每个雪人由三个尺寸各异的雪球构成。通过使用优先队列维护出现次数最多的雪球尺寸,该算法有效地解决了这一问题。
8万+

被折叠的 条评论
为什么被折叠?



