cf - 140 C. New Year Snowmen(贪心+优先队列)

C. 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 n snowballs 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 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 r1r2, ...,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

题意:给你n个已知半径的小球,半径互不相同的三个雪球可以堆成一个雪人,每个雪球只能用一次,求最多可以堆几个雪人并从大到小输出雪球的半径,如果数目一样,随便输出分配方案。
思路:很自然的会想到 O(n^3) 的暴力求解,肯定不行了。

仔细想想这组数据:

14
1 1 2 2 3 3 4 4 4 4 5 5 5 5

怎样分配才是最优呢,如果从1开始选,最终分配只有(1 2 3)(1 2 3)这两种,必然不会最优。如果从5考虑呢 (5 4 3) (5 4 3) ((5 4 2)|| (5 4 1))*2。会有四种情况出现,从4考虑呢,会发现也是这么多种。那么结果出来了,从仙童半径的雪球个数多的先考虑,依次向下考虑不同的半径。
利用map统计相同半径的个数,利用结构体存半径以及个数,利用优先队列按半径的数目从多到少存结构体。每次pop三个,并将 num-1,如果 num != 0,继续push,否则舍弃。

代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 struct Node{
 5     int arr,num;        //半径,数目
 6 }node[100005];
 7 map <int,int> M;
 8 priority_queue <Node> Q;        //默认大根队列,先出队列的为最大的
 9 vector <int> V[3];
10 
11 bool operator < (Node a,Node b){
12     return a.num < b.num;
13 }
14 int main()
15 {
16     ios_base::sync_with_stdio(0);
17     cin.tie(0);
18 
19     int n;
20     while(cin>>n){
21         while(!Q.empty())
22             Q.pop();
23         M.clear();
24         for(int i = 0;i < 3;i ++)   V[i].clear();
25 
26         int temp;
27         for(int i = 1;i <= n;i ++){
28             cin>>temp;
29             M[temp]++;
30         }
31 
32         int len = 0;
33         map <int,int>::iterator it;
34         for(it = M.begin();it != M.end();it ++){        //结构体存半径和数目
35             node[len].arr = it->first;
36             node[len++].num = it->second;
37         }
38         for(int i = 0;i < len;i ++)     //入队列
39             Q.push(node[i]);
40 
41         //cout<<"Check 2"<<endl;
42         len = 0;
43         while(!Q.empty()){
44             Node res1 = Q.top();
45             Q.pop();
46             if(Q.empty())   break;
47             Node res2 = Q.top();
48             Q.pop();
49             if(Q.empty())   break;
50             Node res3 = Q.top();
51             Q.pop();
52 
53             //cout<<res1.num<<"  "<<res2.num<<"  "<<res3.num<<endl;
54 
55             int temp[3] = {res1.arr,res2.arr,res3.arr};
56             sort(temp,temp+3);
57             V[0].push_back(temp[2]);
58             V[1].push_back(temp[1]);
59             V[2].push_back(temp[0]);
60 
61             res1.num--,res2.num--,res3.num--;
62             if(res1.num)   Q.push(res1);        //数目不为0时继续入队列
63             if(res2.num)   Q.push(res2);
64             if(res3.num)   Q.push(res3);
65         }
66         cout<<V[0].size()<<endl;
67         for(int i = 0;i < V[0].size();i ++)
68             cout<<V[0][i]<<" "<<V[1][i]<<" "<<V[2][i]<<endl;
69     }
70     return 0;
71 }
View Code

 





 

转载于:https://www.cnblogs.com/Jstyle-continue/p/6351931.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值