codeforces 140C New Year Snowmen
2016-07-27 20:36
176 查看
New Year Snowmen
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 whatmaximum 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.
Examples
input
7 1 2 3 4 5 6 7
output
2 3 2 1 6 5 4
input
3 2 2 3
output
0
#include<cstdio> #include<vector> #include<cstring> #include<queue> #include<algorithm> using namespace std; struct Answer { int idx1,idx2,idx3; Answer(){} Answer(int _idx1,int _idx2,int _idx3) { idx1=_idx1,idx2=_idx2,idx3=_idx3; if(idx1<idx2)swap(idx1,idx2); if(idx1<idx3)swap(idx1,idx3); if(idx2<idx3)swap(idx2,idx3); } void show(){printf("%d %d %d\n",idx1,idx2,idx3);} }; struct Node { int cnt,val; bool operator < (const Node& t)const { return cnt<t.cnt; } }; const int N=1e5+5; int r ; Node cnt ; vector<Answer>ans; priority_queue<Node>pq; int min(Node a,Node b,Node c) { return min(a.cnt,min(b.cnt,c.cnt)); } int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&r[i]); sort(r+1,r+n+1); int id=0; for(int i=1;i<=n;i++) { if(r[i]!=r[i-1])cnt[++id].val=r[i]; cnt[id].cnt++; } for(int i=1;i<=id;i++) pq.push(cnt[i]); if((int)pq.size()<3) { puts("0"); return 0; } while(1) { Node p1,p2,p3; p1=pq.top();pq.pop(); p2=pq.top();pq.pop(); p3=pq.top();pq.pop(); if(p1.cnt>0&&p2.cnt>0&&p3.cnt>0) ans.push_back(Answer(p1.val,p2.val,p3.val)); else break; p1.cnt--,p2.cnt--,p3.cnt--; pq.push(p1),pq.push(p2),pq.push(p3); } int len=ans.size(); printf("%d\n",len); for(int i=0;i<len;i++)ans[i].show(); return 0; }
相关文章推荐
- 2016暑假集训7.27 链表 数据结构上机测试1:顺序表的应用
- C# 创建ftp连接的时候报 System.Net.Sockets.SocketException] = {"不知道这样的主机。"}
- 完整java开发中JDBC连接数据库代码和步骤
- jenkins+maven+svn+tomcat之快速部署初探(docker篇)
- hdu2222 ac自动机模版
- diff命令
- HDU 5752 Sqrt Bo
- TortoiseSVN菜单项功能说明
- Leetcode 229. Majority Element II (Medium) (cpp)
- 20145202 《Java程序设计》第四周学习总结
- __bridge的相关使用
- python pandas 将行合并
- UVA 539 The Settlers of Catan
- linux 任务计划
- 新手学Linux命令之history
- XSS——突破注释
- 【每周至少一篇 160727】简单的选择问题(select problem)_Java
- 最近公共祖先lca离线
- 整数划分DP
- linux基础(6)-shell编程