题目描述
You have three piles of candies: red, green and blue candies:
the first pile contains only red candies and there are candies in it,
the second pile contains only green candies and there are candies in it,
the third pile contains only blue candies and there are candies in it.
Each day Tanya eats exactly two candies of different colors. She is free to choose the colors of eaten candies: the only restriction that she can’t eat two candies of the same color in a day.
Find the maximal number of days Tanya can eat candies? Each day she needs to eat exactly two candies.
输入
The first line contains integer — the number of test cases in the input.
Then test cases follow.
Each test case is given as a separate line of the input. It contains three integers , and — the number of red, green and blue candies, respectively.
输出
Print integers: the -th printed integer is the answer on the -th test case in the input.
题目大意
给定组测试数据。
每组数据包含三个数,代表3种糖的数量。
每天只能吃不同的两种糖,两种各吃一颗。
求最多能吃多少天。
解法
容易发现哪种颜色都没有关系,因此直接假定:
那么怎么吃最优呢?最优吃法应当是固定的。我考虑计算的差值。
设,
那么若,即把b和r一起吃完b后,只剩下,此时,那么再一起吃天即可。
此时。
若,那么我们把和一起吃,吃天,使得吃完后与相等。
随后均分剩余的给另外两堆,每堆吃天。这里向下取整,如果多了一颗糖不能凑成一对,对结果没有影响。
随后吃天即可把剩下两堆一起吃完。
若为奇数,那么最后会剩下一颗糖,否则全部吃完。
统计答案,
即
整理得
除以二向下取整,那么奇偶的影响就被消除了。可以手推几组感受一下。
Code
#include <cstdio>
#include <algorithm>
using namespace std;
int all[4];
int ans, t;
int main()
{
scanf("%d", &t);
while (t--)
{
ans = 0;
for (int i = 1; i <= 3; ++i)
scanf("%d", all + i);
sort(all + 1, all + 4);
int delta = all[3] - all[2];
if (delta >= all[1])
printf("%d
", all[1] + all[2]);
else
printf("%d
",(all[1] + all[2] + all[3])>>1);
}
return 0;
}