[题目链接]
https://codeforces.com/problemset/problem/140/C
[算法]
显然 , 我们每次应优先考虑数量多的雪球
将雪球个数加入堆中 , 每次取出数量前三大的雪球 , 贪心地将它们分到一个组中即可
时间复杂度 : O(N log N)
[代码]
#include<bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; struct info { int v1 , v2 , v3; } ans[MAXN]; int n , tot; int r[MAXN]; map< int,int > mp; priority_queue< pair<int,int> > q; template <typename T> inline void read(T &x) { T f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } int main() { read(n); for (int i = 1; i <= n; i++) { read(r[i]); mp[r[i]]++; } for (map< int,int > :: iterator it = mp.begin(); it != mp.end(); it++) q.push(make_pair(it -> second,it -> first)); while ((int)q.size() >= 3) { pair<int,int> a = q.top(); q.pop(); pair<int,int> b = q.top(); q.pop(); pair<int,int> c = q.top(); q.pop(); ans[++tot] = (info){a.second,b.second,c.second}; if (a.first > 1) q.push(make_pair(a.first - 1,a.second)); if (b.first > 1) q.push(make_pair(b.first - 1,b.second)); if (c.first > 1) q.push(make_pair(c.first - 1,c.second)); } printf("%d ",tot); for (int i = 1; i <= tot; i++) { if (ans[i].v1 < ans[i].v2) swap(ans[i].v1,ans[i].v2); if (ans[i].v1 < ans[i].v3) swap(ans[i].v1,ans[i].v3); if (ans[i].v2 < ans[i].v3) swap(ans[i].v2,ans[i].v3); printf("%d %d %d ",ans[i].v1,ans[i].v2,ans[i].v3); } return 0; }