标签:
题目大意:有n个已知半径的雪球。堆一个雪人需要三个尺寸不同的雪球,问用这些雪球最多能堆多少个雪人?
题目分析:先统计一下每种尺寸的球的个数,从三种最多的种类中各取出一个堆成雪人,这样贪心能保证的到的数目最多。
代码如下:
# include<iostream> # include<map> # include<vector> # include<cstdio> # include<queue> # include<algorithm> using namespace std; struct Node { int val,cnt; Node(int _val,int _cnt):val(_val),cnt(_cnt){} bool operator < (const Node &a) const{ return cnt<a.cnt; } }; int ans[40005][3],n; map<int,int>mp; priority_queue<Node>q; void solve() { int cnt=0; while(!q.empty()){ Node a=q.top(); q.pop(); if(q.empty()) break; Node b=q.top(); q.pop(); if(q.empty()) break; Node c=q.top(); q.pop(); ans[cnt][0]=a.val; ans[cnt][1]=b.val; ans[cnt][2]=c.val; ++cnt; if(a.cnt-1>0) q.push(Node(a.val,a.cnt-1)); if(b.cnt-1>0) q.push(Node(b.val,b.cnt-1)); if(c.cnt-1>0) q.push(Node(c.val,c.cnt-1)); } printf("%d\n",cnt); for(int i=0;i<cnt;++i){ sort(ans[i],ans[i]+3); printf("%d %d %d\n",ans[i][2],ans[i][1],ans[i][0]); } } int main() { int a; while(~scanf("%d",&n)) { mp.clear(); for(int i=0;i<n;++i){ scanf("%d",&a); ++mp[a]; } map<int,int>::iterator it; while(!q.empty()) q.pop(); for(it=mp.begin();it!=mp.end();++it){ q.push(Node(it->first,it->second)); } solve(); } return 0; }
CoderForce 140C-New Year Snowmen(贪心)
标签:
原文地址:http://www.cnblogs.com/20143605--pcx/p/5164727.html