您的位置:首页 > 其它

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

2016-07-28 10:30 549 查看
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 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


题意:给你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,否则舍弃。

代码:

#include <bits/stdc++.h>
using namespace std;

struct Node{
int arr,num;        //半径,数目
}node[100005];
map <int,int> M;
priority_queue <Node> Q;        //默认大根队列,先出队列的为最大的
vector <int> V[3];

bool operator < (Node a,Node b){
return a.num < b.num;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);

int n;
while(cin>>n){
while(!Q.empty())
Q.pop();
M.clear();
for(int i = 0;i < 3;i ++)   V[i].clear();

int temp;
for(int i = 1;i <= n;i ++){
cin>>temp;
M[temp]++;
}

int len = 0;
map <int,int>::iterator it;
for(it = M.begin();it != M.end();it ++){        //结构体存半径和数目
node[len].arr = it->first;
node[len++].num = it->second;
}
for(int i = 0;i < len;i ++)     //入队列
Q.push(node[i]);

//cout<<"Check 2"<<endl;
len = 0;
while(!Q.empty()){
Node res1 = Q.top();
Q.pop();
if(Q.empty())   break;
Node res2 = Q.top();
Q.pop();
if(Q.empty())   break;
Node res3 = Q.top();
Q.pop();

//cout<<res1.num<<"  "<<res2.num<<"  "<<res3.num<<endl;

int temp[3] = {res1.arr,res2.arr,res3.arr};
sort(temp,temp+3);
V[0].push_back(temp[2]);
V[1].push_back(temp[1]);
V[2].push_back(temp[0]);

res1.num--,res2.num--,res3.num--;
if(res1.num)   Q.push(res1);        //数目不为0时继续入队列
if(res2.num)   Q.push(res2);
if(res3.num)   Q.push(res3);
}
cout<<V[0].size()<<endl;
for(int i = 0;i < V[0].size();i ++)
cout<<V[0][i]<<" "<<V[1][i]<<" "<<V[2][i]<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: