题目描述
You have three piles of candies: red, green and blue candies:
the first pile contains only red candies and there are
r
r
r candies in it,
the second pile contains only green candies and there are
g
g
g candies in it,
the third pile contains only blue candies and there are
b
b
b 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
t
(
1
≤
t
≤
1000
)
t (1≤t≤1000)
t(1≤t≤1000) — the number of test cases in the input.
Then
t
t
t test cases follow.
Each test case is given as a separate line of the input. It contains three integers r r r, g g g and b ( 1 ≤ r , g , b ≤ 1 0 8 ) b(1≤r,g,b≤10^8) b(1≤r,g,b≤108) — the number of red, green and blue candies, respectively.
输出
Print t t t integers: the i i i-th printed integer is the answer on the i i i-th test case in the input.
题目大意
给定
t
t
t组测试数据。
每组数据包含
r
,
g
,
b
r,g,b
r,g,b三个数,代表3种糖的数量。
每天只能吃不同的两种糖,两种各吃一颗。
求最多能吃多少天。
解法
容易发现哪种颜色都没有关系,因此直接假定:
r
>
g
>
b
r>g>b
r>g>b
那么怎么吃最优呢?最优吃法应当是固定的。我考虑计算
r
与
g
r与g
r与g的差值。
设
d
e
l
t
a
=
r
−
g
delta = r - g
delta=r−g,
那么若
d
e
l
t
a
>
=
b
delta >= b
delta>=b,即把b和r一起吃完b后,只剩下
r
−
b
,
g
r-b,g
r−b,g,此时
r
−
b
>
=
g
r-b>=g
r−b>=g,那么再一起吃
g
g
g天即可。
此时
a
n
s
=
b
+
g
ans = b + g
ans=b+g。
若
d
e
l
t
a
<
b
delta < b
delta<b,那么我们把
r
r
r和
b
b
b一起吃,吃
d
e
l
t
a
delta
delta天,使得
r
r
r吃完后与
g
g
g相等。
随后均分剩余的
b
b
b给另外两堆,每堆吃
(
b
−
d
e
l
t
a
)
/
2
(b-delta)/2
(b−delta)/2天。这里向下取整,如果多了一颗糖不能凑成一对,对结果没有影响。
随后吃
g
−
(
b
−
d
e
l
t
a
)
/
2
g-(b-delta)/2
g−(b−delta)/2天即可把剩下两堆一起吃完。
若
(
b
−
d
e
l
t
a
)
/
2
(b-delta)/2
(b−delta)/2为奇数,那么最后会剩下一颗糖,否则全部吃完。
统计答案,
a
n
s
=
d
e
l
t
a
+
(
b
−
d
e
l
t
a
)
+
g
−
(
b
−
d
e
l
t
a
)
/
2
ans = delta + (b-delta) + g-(b-delta)/2
ans=delta+(b−delta)+g−(b−delta)/2
即
a
n
s
=
r
−
g
+
b
−
r
+
g
+
g
−
b
/
2
+
r
/
2
−
g
/
2
ans = r - g + b - r + g + g - b/2 + r/2 - g/2
ans=r−g+b−r+g+g−b/2+r/2−g/2
整理得
a
n
s
=
(
r
+
g
+
b
)
/
2
ans = (r+g+b)/2
ans=(r+g+b)/2
除以二向下取整,那么奇偶的影响就被消除了。可以手推几组感受一下。
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\n", all[1] + all[2]);
else
printf("%d\n",(all[1] + all[2] + all[3])>>1);
}
return 0;
}