[CodeForces]1263A Sweet Problem

题目链接

题目描述

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(1t1000) — 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(1r,g,b108) — 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 rg的差值。
d e l t a = r − g delta = r - g delta=rg

那么若 d e l t a > = b delta >= b delta>=b,即把b和r一起吃完b后,只剩下 r − b , g r-b,g rb,g,此时 r − b > = g r-b>=g rb>=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 (bdelta)/2天。这里向下取整,如果多了一颗糖不能凑成一对,对结果没有影响。
随后吃 g − ( b − d e l t a ) / 2 g-(b-delta)/2 g(bdelta)/2天即可把剩下两堆一起吃完。
( b − d e l t a ) / 2 (b-delta)/2 (bdelta)/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+(bdelta)+g(bdelta)/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=rg+br+g+gb/2+r/2g/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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值