[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;
}
### 关于 Codeforces 上二项装箱问题 #### 二项装箱问题概述 二项装箱问题是经典的组合优化问题之一,在计算机科学领域具有重要意义。该类问题通常涉及将一组不同大小的对象放入固定容量的容器中,目标是最小化使用的容器数量[^1]。 对于特定平台上的挑战实例,如Codeforces中的二项装箱问题,其核心在于设计高效算法来解决这一NP难问题。尽管找到最优解可能非常复杂,但存在多种启发式方法可以提供接近最佳的结果,并且这些方法能够在合理的时间内执行完毕。 #### 解决方案策略 一种常见的处理方式是采用贪心算法,即总是尝试把当前最大的未分配物品放置到第一个能够容纳它的箱子中;如果没有任何现有箱子能放下这件物品,则创建一个新的箱子用于装载它。这种方法简单易懂,但在某些情况下可能会导致次优解。 更复杂的近似算法包括首次适应下降(First Fit Decreasing, FFD),此技术首先按照降序排列所有项目尺寸,之后应用首次适配原则(FD)。FFD已被证明能在多项式时间内给出不超过理想最小值11/9倍数加四的解法质量保证[^2]。 此外还有其他高级求解途径比如动态规划、分支限界以及遗传算法等,它们各自适用于不同的应用场景并提供了不同程度上的性能改进。 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n; cin >> n; vector<int> items(n); for(int i = 0; i < n; ++i){ cin >> items[i]; } sort(items.begin(), items.end(), greater<int>()); const int bin_capacity = 1000; // 假设每个bin的最大容量为1000单位体积 vector<int> bins; for(auto item : items){ bool placed = false; for(auto& b : bins){ if(b + item <= bin_capacity){ b += item; placed = true; break; } } if(!placed){ bins.push_back(item); } } cout << "Minimum number of bins required is: " << bins.size(); } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值