您的位置:首页 > 其它

CodeForces - 140C New Year Snowmen(贪心)

2018-03-05 16:34 239 查看
New Year SnowmenAs 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.InputThe 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.OutputPrint 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.ExamplesinputCopy
7
1 2 3 4 5 6 7
output
2
3 2 1
6 5 4
inputCopy
3
2 2 3
output
0
题意:给出n个雪球,每个雪球半径为Ri,不同半径的3个小球可以组成一个雪人,问最多可以组成多少个雪人?
思路:wa了一发后,想了想这个跟雪球的数目有明显的关系,我们其实应该优先用多的那个半径的雪球,因为某个半径多的话,它就有更多的机会与别的球结合,酱就会组成尽量多的雪人.(介于题目情况,我用了map和优先队列)
当然,二分求解也是可以的,假设要组成mid个雪人,那么某个半径的雪球我们最多只能选择mid个(想想是吧,因为雪人一共就由3*mid组成,他只能做最大或者中间大或者小的),然后二分完输出就好.
二分代码://二分
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;

int n;
int ans[maxn][3];
map<int,int> mp;

int main()
{
cin>>n;
for(int i = 1;i<= n;i++)
{
int x;
scanf("%d",&x);
mp[x]++;
}

int l = 1,r = n/3;
int maxm = 0;
while(l<= r)
{
int mid = (l+r)>>1;
int tmp = 0;

for(it = mp.begin();it!= mp.end();it++)
tmp+= min(it->second,mid);

if(tmp>= mid*3)
l = mid+1,maxm = mid;
else
r = mid-1;
}

cout<<maxm<<endl;
int i = 0,j = 0;
for(it = mp.begin();it!= mp.end();it++)
{
int k = min(it->second,maxm);
while(k--)
{
ans[++i][j] = it->first;
if(i == maxm)
{
j++;
i = 0;
}
if(j == 3)
break;
}
if(j == 3)
break;
}

for(int i = 1;i<= maxm;i++)
printf("%d %d %d\n",ans[i][2],ans[i][1],ans[i][0]);

return 0;
}

贪心代码://贪心+优先队列
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;

struct node
{
int num;
int sum;
node (int num = 0,int sum = 0): num(num),sum(sum){}
};
int n,ans[maxn][3];
map<int,int> mp;
bool operator< (node x,node y)
{
return x.sum< y.sum;
}

int main()
{
cin>>n;
for(int i = 1;i<= n;i++)
{
int x;
scanf("%d",&x);
mp[x]++;
}

priority_queue<node> q;
for(it = mp.begin();it!= mp.end();it++)
q.push(node(it->first,it->second));

int cnt = 0;
while(q.size()>= 3)
{
node a = q.top();
q.pop();
node b = q.top();
q.pop();
node c = q.top();
q.pop();

int tmp[3] = {a.num,b.num,c.num};
sort(tmp,tmp+3);

ans[++cnt][0] = tmp[2];
ans[cnt][1] = tmp[1];
ans[cnt][2] = tmp[0];

a.sum--;
b.sum--;
c.sum--;
if(a.sum)
q.push(a);
if(b.sum)
q.push(b);
if(c.sum)
q.push(c);
}

cout<<cnt<<endl;
for(int i = 1;i<= cnt;i++)
printf("%d %d %d\n",ans[i][0],ans[i][1],ans[i][2]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息