HDU 3573 Buy Sticks 解题报告(贪心)

来源:互联网 发布:中网络电视台 编辑:程序博客网 时间:2024/06/07 01:28

Buy Sticks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 727    Accepted Submission(s): 316


Problem Description
Imyourgod need 3 kinds of sticks which have different sizes: 20cm, 28cm and 32cm. However the shop only sell 75-centimeter-long sticks. So he have to cut off the long stick. How many sticks he must buy at least.
 

Input
The first line of input contains a number t, which means there are t cases of the test data.
There will be several test cases in the problem, each in one line. Each test cases are described by 3 non-negtive-integers separated by one space representing the number of sticks of 20cm, 28cm and 32cm. All numbers are less than 10^6.
 

Output
The output contains one line for each line in the input case. This line contains the minimal number of 75-centimeter-long sticks he must buy. Format are shown as Sample Output.
 

Sample Input
23 1 14 2 2
 

Sample Output
Case 1: 2Case 2: 3
 

Author
imyourgod (Special Thanks : crackerwang & Louty)
 

Source
2010 ACM-ICPC Multi-University Training Contest(14)——Host by BJTU
 

    解题报告:题意很容易理解。买20,28,32三种棒,但是商店里只有75cm的,所以买75cm的裁剪,裁剪剩下来的不可以拼接。
    给定20,28,32cm棒需要的数量x,y,z,求最少买多少根75cm的棒。
    75cm的棒可以裁剪成以下的方式:
    3,0,0  -》 a
    0,2,0  -》 b
    0,0,2  -》 c
    2,1,0  -》 d
    2,0,1  -》 e
    0,1,1  -》 f
    假设每种按照裁剪方式的裁剪的棒数量为a,b,c,d,e,f,那么我们可以得出:
    3*a + 2*d + 2*e = x (1)
    2*b + d + f = y (2)
    2*c + e + f = z (3)
    而所求的最少的棒即为min(a+b+c+d+e+f)。
    (1)(2)(3)三个式子相加,得到 3*a+2*b+2*c+3*d+3*e+2*f = x+y+z。(4)
    (4)式两边同加上(b+c+f),得到3*(a+b+c+d+e+f) = x+y+z+b+c+f。(5)
    由(5)式我们可以知道,求min(a+b+c+d+e+f)即为求min(b+c+f)+x+y+z。
    (2)(3)式相加,得到2*(b+c+f) = y+z-(d+e)。(6)
    由(5)(6)得到,6*(a+b+c+d+e+f) = 2*x+3*y+3*z-(d+e)。
    由此,即为求(d+e)的最大值。按照此贪心即可。
    代码如下:
#include <cstdio>#include <algorithm>int T,cas,a,b,c;int main(){    scanf("%d",&T);    while(T--)    {        scanf("%d%d%d",&a,&b,&c);        printf("Case %d: %d\n",++cas,(2*a+3*b+3*c-std::min((a+1)/2,b+c)+5)/6);    }}
    杭电最短代码,哈哈。

0 0