new year sonwmen

来源:互联网 发布:win10禁止安装软件 编辑:程序博客网 时间:2024/06/06 23:19
New Year Snowmen
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 r1r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 12 and 3 can be used to make a snowman but 223 or 222 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.

<p< p="" style="color: rgb(34, 34, 34); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 21px; "><p< p="">
Input
<p< p="">

The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radiir1r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.

<p< p=""><p< p="">
Output
<p< p="">

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.

<p< p=""><p< p="">
Sample test(s)
<p< p=""><p< p="">
input
71 2 3 4 5 6 7
output
23 2 16 5 4
input
32 2 3
output
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;}


原创粉丝点击