new year sonwmen
来源:互联网 发布:win10禁止安装软件 编辑:程序博客网 时间:2024/06/06 23:19
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 nsnowballs 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.
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radiir1, 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.
71 2 3 4 5 6 7
23 2 16 5 4
32 2 3
0
觉得是个很棒的题目,题意就是给出n个数,然后选择求出最大的个数m,每个m都是由3个大小不同的数构成的(也就是三个数大小不同)。
可以用map<int,int>容器事先把这个数与这个数的个数对应起来,因为map默认的是将数从小到大排列,而对应的个数是将从大到小排列。
假设这n个数都能构成,那么m=n/3,然后用二分可以求出实际的最大个数ansm,因为每个数最多只能取当前的sum+=min(这个数的个数,当前的最大个数ansm);
求出了ansm剩下的好办了,因为map容器已经排好了序列,只要一层层的把出现的数铺上去就可以了。
具体代码如下:
#include <iostream>#include <stdio.h>#include<windows.h>#include <cstring>#include <map>using namespace std;int main(){int i,j,k,l,n,ans;int a[40000][3];map<int, int> M;map<int, int>::iterator it;cin>>n;for (i=0; i<n;i++) {cin>>j; M[j]++;}/*for(it=M.begin();it!=M.end();it++){cout<<it->first<<" "<<it->second<<endl; } */int L=0, R=n/3;while (L<R){ans=(L+R+1)/2;int cnt=0;for (it=M.begin();it!=M.end();it++) cnt+=min(it->second,ans);if (cnt>=3*ans) L=ans; else R=ans-1;}ans=L;cout<<ans<<endl;i=0; j=0;for (it=M.begin(); it!=M.end();it++){k=min(it->second,ans);while (k--){a[i][j]=it->first;i++;if (i==ans) {j++;i=0;}if (j==3) break; }if (j==3) break;}for ( i=0; i<ans; i++)cout<<a[i][2]<<" "<<a[i][1]<<" "<<a[i][0]<<endl;return 0;}
- new year sonwmen
- new year~~
- new year
- new year
- new year
- new year
- new year
- New Year, New Start
- New Year, New Life
- 2006 New year New hope
- Happy New Year!
- Happ New Year
- ~~New Year,My Paradise!!~~
- Happy new year!
- happy new year
- New Year Thoughts
- For a New Year
- Happy New Year !
- Java 以指定日期时间格式获取当前时间、以及每隔一秒刷新一次的方法------附带实例
- IE6:支持非a标签的hover属性
- Programming Clojure学习笔记——并发
- ArcGIS Desktop Add-in插件开发系列四
- MonoBehaviour.Invoke 调用
- new year sonwmen
- HDOJ 1019 多个数的最小公倍数
- Programming Clojure学习笔记——宏
- Programming Clojure学习笔记——宏
- 网络通信程序写起来很难专业课没问题
- 简单炮弹的克隆发射
- android即时通讯开发经验谈
- myeclipse6.5注册码
- android 汉化