You have r red, g green and b blue balloons. To decorate a single table for the banquet you need exactly three balloons. Three balloons attached to some table shouldn't have the same color. What maximum number t of tables can be decorated if we know number of balloons of each color?
Your task is to write a program that for given values r, g and b will find the maximum number t of tables, that can be decorated in the required manner.
The single line contains three integers r, g and b (0 ≤ r, g, b ≤ 2·109) — the number of red, green and blue baloons respectively. The numbers are separated by exactly one space.
Print a single integer t — the maximum number of tables that can be decorated in the required manner.
5 4 3
4
1 1 1
1
2 3 3
2
In the first sample you can decorate the tables with the following balloon sets: "rgg", "gbb", "brr", "rrg", where "r", "g" and "b" represent the red, green and blue balls, respectively.
题意:已知红色,绿色,蓝色的气球的数目,装扮一个桌子要三个气球,每个桌子的气球颜色数必须大于等于2。求最多能装扮多少个桌子?
题解:一种较简单的做法,假设r>=g>=b。如果 r>=2*(b+g) ,那么答案是 b+g ,否则,答案是(b+g+r) / 3 。细想一下,只有r>2*(b+g)的时候,剩下的气球才可能超过3个。
我的做法是,先让每张桌子三个气球的颜色一模一样。然后通过贪心地交换桌子间的气球,让尽量多的桌子满足条件。这样最后可能还剩下一些气球,我们再特殊处理就是了。代码如下:
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<iostream>
#include<string.h>
#include<string>
#include<math.h>
#include<stdlib.h>
#define inff 0x3fffffff
#define eps 1e-8
#define nn 510000
#define mod 1000000007
typedef long long LL;
const LL inf64=LL(inff)*inff;
using namespace std;
LL a[4];
LL b[4];
int main()
{
int i;
while(cin>>a[1]>>a[2]>>a[3])
{
sort(a+1,a+4);
LL ans=0;
for(i=1;i<=3;i++)
b[i]=a[i]%3;
for(i=1;i<=3;i++)
a[i]/=3;
ans+=(a[1]+a[2]);
LL ix=max(a[3]-a[1]-a[2],LL(0));
ix-=min(ix,a[1]+a[2]);
ans+=a[3]-ix;
LL fc=min(ix,b[1]+b[2]);
ix-=fc;
b[3]+=fc;
ans+=fc;
LL wy=b[1]+b[2]-fc;
if(b[3]<3)
{
ans+=(b[3]+wy)/3;
}
else
{
fc=min(b[3]/2,wy);
ans+=fc;
b[3]-=fc*2;
wy-=fc;
if(b[3]>2)
b[3]=2;
ans+=(wy+b[3])/3;
}
cout<<ans<<endl;
}
return 0;
}