题目链接

题目描述

You have three piles of candies: red, green and blue candies:

the first pile contains only red candies and there are rrr candies in it,

the second pile contains only green candies and there are ggg candies in it,

the third pile contains only blue candies and there are bbb 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 ttt test cases follow.

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

输出

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

题目大意

给定ttt组测试数据。

每组数据包含r,g,br,g,br,g,b三个数,代表3种糖的数量。

每天只能吃不同的两种糖,两种各吃一颗。

求最多能吃多少天。

解法

容易发现哪种颜色都没有关系,因此直接假定:

r>g>br>g>br>g>b

那么怎么吃最优呢?最优吃法应当是固定的。我考虑计算r与gr与gr与g的差值。

设delta=r−gdelta = r - gdelta=r−g,

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

此时ans=b+gans = b + gans=b+g。

若delta<bdelta < bdelta<b,那么我们把rrr和bbb一起吃,吃deltadeltadelta天,使得rrr吃完后与ggg相等。

随后均分剩余的bbb给另外两堆,每堆吃(b−delta)/2(b-delta)/2(b−delta)/2天。这里向下取整,如果多了一颗糖不能凑成一对,对结果没有影响。

随后吃g−(b−delta)/2g-(b-delta)/2g−(b−delta)/2天即可把剩下两堆一起吃完。

若(b−delta)/2(b-delta)/2(b−delta)/2为奇数,那么最后会剩下一颗糖,否则全部吃完。

统计答案,ans=delta+(b−delta)+g−(b−delta)/2ans = delta + (b-delta) + g-(b-delta)/2ans=delta+(b−delta)+g−(b−delta)/2

即ans=r−g+b−r+g+g−b/2+r/2−g/2ans = r - g + b - r + g + g - b/2 + r/2 - g/2ans=r−g+b−r+g+g−b/2+r/2−g/2

整理得ans=(r+g+b)/2ans = (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\n", all[1] + all[2]);
else
printf("%d\n",(all[1] + all[2] + all[3])>>1);
}
return 0;
}

[CodeForces]1263A Sweet Problem的更多相关文章

  1. Codeforces Round #603 (Div. 2) A. Sweet Problem(水.......没做出来)+C题

    Codeforces Round #603 (Div. 2) A. Sweet Problem A. Sweet Problem time limit per test 1 second memory ...

  2. Codeforces Round #603 (Div. 2) A. Sweet Problem 水题

    A. Sweet Problem the first pile contains only red candies and there are r candies in it, the second ...

  3. codeforces 340C Tourist Problem

    link:http://codeforces.com/problemset/problem/340/C 开始一点也没思路,赛后看别人写的代码那么短,可是不知道怎么推出来的啊! 后来明白了. 首先考虑第 ...

  4. codeforces B. Routine Problem 解题报告

    题目链接:http://codeforces.com/problemset/problem/337/B 看到这个题目,觉得特别有意思,因为有熟悉的图片(看过的一部电影).接着让我很意外的是,在纸上比划 ...

  5. Codeforces 527D Clique Problem

    http://codeforces.com/problemset/problem/527/D 题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集 ...

  6. Codeforces 706C - Hard problem - [DP]

    题目链接:https://codeforces.com/problemset/problem/706/C 题意: 给出 $n$ 个字符串,对于第 $i$ 个字符串,你可以选择花费 $c_i$ 来将它整 ...

  7. Codeforces 1096D - Easy Problem - [DP]

    题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符 ...

  8. Codeforces 793C - Mice problem(几何)

    题目链接:http://codeforces.com/problemset/problem/793/C 题目大意:给你一个捕鼠器坐标,和各个老鼠的的坐标以及相应坐标的移动速度,问你是否存在一个时间点可 ...

  9. CodeForces 687A NP-Hard Problem

    Portal:http://codeforces.com/problemset/problem/687/A 二分图染色 好模板题 有SPJ 值得注意的是,因为C++的奇妙的运算机制 若在vector变 ...

随机推荐

  1. python开发接口

    享一段代码,开发了3个接口:            1.上传文件            2.查看所有文件            3.下载文件 使用python开发,需要安装flask模块,使用pip ...

  2. Can you answer these queries?-HDU4027 区间开方

    题意: 给你n个数,两个操作,0为区间开方,1为区间求和 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 思路: 如果当该区间的数都为1,我们没必要 ...

  3. springMVC的 Converter转换器 和 Formatter

    Converter转换器 spring的Converter是可以将一种类型转换成另一种类型的一个对象, 自定义Converter需要实现Converter接口 日期转换器 import java.te ...

  4. C++ 知识零碎搭建

    全局变量 局部变量 函数不能嵌套定义 C/C++ 变量在将要被使用时定义即可, 不必一开始就声明所有变量 函数的定义与声明的区别 C++常规类型自动类型转换规则 C语言中十六进制和八进制的格式: 二进 ...

  5. Windows驱动开发-手动创建IRP

    手动创建IRP有以下几个步骤: 1,先得到设备的指针,一种方法是用IoGetDeviceObjectPointer内核函数得到设备对象指针,另外一种方法是用zwCreateFile内核函数先得到设备句 ...

  6. Linux:vi & vim(待学)

    VI编辑器_终端编辑器 目标 vi简介 打开和新建文件 三种工作模式 常用命令查询 1 简介 1.1 学习vi的目的 在工作中, 要对 服务器上的 文件进行 简单 的修改, 可以使用 ssh 登录到远 ...

  7. Set和Map集合的比较

     HashSet:数据进行hashCode比较,然后进行equals方法比较,根据比较结果进行排序.如果要对对象进行排序,对象类要重写hashCode和equals方法.TreeSet:如果要对对象进 ...

  8. c#能同时继承接口和类吗

    c#能同时继承接口和类吗?( 要你命3000条12级分类:C#/.NET语言被浏览449次2013.09.10   满意答案 mroyal450 采纳率:54%12级 2013.09.11 C# 类, ...

  9. 学习不一样的vue实战(2): 项目分析

    学习不一样的vue实战(2): 项目分析 首先 首发博客: 我的博客 项目源码: 源码(喜欢请star) 项目预览: 预览 上一章: <学习不一样的vue(1)实战:环境搭建> 我的Q群: ...

  10. linux#自启动脚本

    编写脚本: /etc/init.d/myscriptname # chkconfig: # description: 描述信息,描述信息,上面的90表示在众多开机启动脚本的优先级,10表示在众多关机启 ...