zoukankan      html  css  js  c++  java
  • [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 rr candies in it,
    the second pile contains only green candies and there are gg candies in it,
    the third pile contains only blue candies and there are bb 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(1t1000)t (1≤t≤1000) — the number of test cases in the input.
    Then tt test cases follow.

    Each test case is given as a separate line of the input. It contains three integers rr, gg and b(1r,g,b108)b(1≤r,g,b≤10^8) — the number of red, green and blue candies, respectively.

    输出

    Print tt integers: the ii-th printed integer is the answer on the ii-th test case in the input.

    题目大意

    给定tt组测试数据。
    每组数据包含r,g,br,g,b三个数,代表3种糖的数量。
    每天只能吃不同的两种糖,两种各吃一颗。
    求最多能吃多少天。

    解法

    容易发现哪种颜色都没有关系,因此直接假定:
    r>g>br>g>b
    那么怎么吃最优呢?最优吃法应当是固定的。我考虑计算rgr与g的差值。
    delta=rgdelta = r - g

    那么若delta>=bdelta >= b,即把b和r一起吃完b后,只剩下rb,gr-b,g,此时rb>=gr-b>=g,那么再一起吃gg天即可。
    此时ans=b+gans = b + g

    delta<bdelta < b,那么我们把rrbb一起吃,吃deltadelta天,使得rr吃完后与gg相等。
    随后均分剩余的bb给另外两堆,每堆吃(bdelta)/2(b-delta)/2天。这里向下取整,如果多了一颗糖不能凑成一对,对结果没有影响。
    随后吃g(bdelta)/2g-(b-delta)/2天即可把剩下两堆一起吃完。
    (bdelta)/2(b-delta)/2为奇数,那么最后会剩下一颗糖,否则全部吃完。
    统计答案,ans=delta+(bdelta)+g(bdelta)/2ans = delta + (b-delta) + g-(b-delta)/2
    ans=rg+br+g+gb/2+r/2g/2ans = r - g + b - r + g + g - b/2 + r/2 - g/2
    整理得ans=(r+g+b)/2ans = (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
    ", all[1] + all[2]);
            else
                printf("%d
    ",(all[1] + all[2] + all[3])>>1);
        }
        return 0;
    }
    
  • 相关阅读:
    JQuery -- this 和 $(this) 的区别
    js动态生成表格
    Sublime 3 如何使用列编辑模式
    新版Sublime text3注册码被移除的解决办法
    Sublime text 3 格式化HTML/css/js/json代码 插件
    mingw64 构建 Geos
    C++调用Python浅析
    linux下挂载VHD等虚拟磁盘文件
    linux 下Qt WebEngine 程序打包简单记录
    C++ UTF8和UTF16互转代码
  • 原文地址:https://www.cnblogs.com/Clouder-Blog/p/12146648.html
Copyright © 2011-2022 走看看