hdu 4112 Break the Chocolate 贪心

Break the Chocolate

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=4112

Description


Benjamin is going to host a party for his big promotion coming up.
Every
party needs candies, chocolates and beer, and of course Benjamin has
prepared some of those. But as everyone likes to party, many more people
showed up than he expected. The good news is that candies are enough.
And for the beer, he only needs to buy some extra cups. The only problem
is the chocolate.
As Benjamin is only a ‘small court officer‘ with
poor salary even after his promotion, he can not afford to buy extra
chocolate. So he decides to break the chocolate cubes into smaller
pieces so that everyone can have some.
He have two methods to break
the chocolate. He can pick one piece of chocolate and break it into two
pieces with bare hand, or put some pieces of chocolate together on the
table and cut them with a knife at one time. You can assume that the
knife is long enough to cut as many pieces of chocolate as he want.
The
party is coming really soon and breaking the chocolate is not an easy
job. He wants to know what is the minimum number of steps to break the
chocolate into unit-size pieces (cubes of size 1 × 1 × 1). He is not
sure whether he can find a knife or not, so he wants to know the answer
for both situations.

Input

The first line contains an integer T(1<= T <=10000), indicating the number of test cases.
Each
test case contains one line with three integers N,M,K(1 <=N,M,K
<=2000), meaning the chocolate is a cube of size N ×M × K.

Output

For each test case in the input, print one line: "Case #X: A B", where X is the test case number (starting with 1) , A and B are the minimum numbers of steps to break the chocolate into N × M × K unit-size pieces with bare hands and knife respectively.

Sample Input

2 1 1 3 2 2 2

Sample Output

Case #1: 2 2 Case #2: 7 3

HINT

题意

有两种切法,一种是一次切一块,一种是一次可以切多块,然后问你在两种情况下,最少切多少下

题解:

第一种就毫无疑问,就是 a*b*c-1,第二种脑补一下,很显然是二分切

然后小心爆int,然后就好了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 50051
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
/*

inline void P(int x)
{
    Num=0;if(!x){putchar(‘0‘);puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
*/
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar(‘0‘);puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************
int deal(int x)
{
    int cnt=0;
    while(1)
    {
        if(1<<cnt>=x)
            return cnt;
        cnt++;
    }
}
int main()
{
    //freopen("test.txt","r",stdin);
    int t=read();
    for(int cas=1;cas<=t;cas++)
    {
        ll a,b,c;
        a=read(),b=read(),c=read();
        printf("Case #%d: %lld %lld\n",cas,a*b*c-1,deal(a)+deal(b)+deal(c));
    }
}
时间: 2024-10-09 18:38:28

hdu 4112 Break the Chocolate 贪心的相关文章

HDU 4112 Break the Chocolate(简单的数学推导)

Break the Chocolate Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4556    Accepted Submission(s): 1458 Problem Description Benjamin is going to host a party for his big promotion coming up.Eve

HDU - 4112 Break the Chocolate

Problem Description Benjamin is going to host a party for his big promotion coming up. Every party needs candies, chocolates and beer, and of course Benjamin has prepared some of those. But as everyone likes to party, many more people showed up than

HDU - 4112 Break the Chocolate(规律)

题意:有一块n*m*k的巧克力,最终需要切成n*m*k个1*1*1的块,问用以下两种方法最少掰多少次能达到目的: 1.用手掰:每次只能拿出一块来掰:2.用刀切:可以把很多已经分开的块摞在一起一刀切下来 分析: 1.用手掰,需要n*m*k-1次. 2.用刀切,可以分别考虑长宽高,计算长宽高分别切成单位长度所需要的最少次数,相加即可. 二分切,可得最少次数.规律为,长度为x最少需切ceil(log2(x))次. #include<cstdio> #include<algorithm>

【贪心专题】HDU 1009 FatMouse&#39; Trade (贪心选取)

链接:click here~~ 题意:老鼠准备了M磅猫食,准备拿这些猫食跟猫交换自己喜欢的食物.有N个房间,每个房间里面都有食物.你可以得到J[i]单位的食物,但你需要付出F[i]单位的的猫食. 计算M磅猫食可以获得最多食物的重量. [解题思路]贪心算法,求最优解.将J[i]/F[i]的值从大到小排列,每次取最大的,局部最优,达到全局最优,从而获得最大值. 代码: // 贪心策略,优先选择投资最大的房间,每选择一次,交换次数依次减少,最后的次数用于价值最小的 //注意精度转化:1.0*(int

HDU 1009 FatMouse&#39; Trade (贪心算法)

题意:就是老鼠要用猫粮换粮食,第i个房间一些东西,要用东西去换,可以不全换.问给定的猫粮最多能换多少粮食. 析:贪心算法.我们先算出来每个房间物品的平均价格是多少,肯定越低越好,并且如果能全换就全换,如果不能, 肯定是最后一次了,就把剩下全部换了,看看能换多少.求和. 代码如下: #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <v

HDU 4923 Room and Moor 贪心+栈

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4923 题意:,Bi可以是小数. 思路:很机智的想法,对于连续M个1+N个0的一块来说,最优解一定是,Bi=M/(M+N),因为Bi是递增的(可以手推),所以如果出现在后面的一块中的Bi>前面一块的Bi,那么就不可能取到最优解,所以将两块合并一起处理,这样过程中就需要用栈来维护了. 代码: #include <iostream> #include <cstdio> #include &

HDU - 3644:A Chocolate Manufacturer&#39;s Problem(模拟退火, 求多边形内最大圆半径)

pro:给定一个N边形,然后给半径为R的圆,问是否可以放进去.  问题转化为多边形的最大内接圆半径.(N<50): sol:乍一看,不就是二分+半平面交验证是否有核的板子题吗. 然而事情并没有那么简单.  因为我们的多边形可能是凹多边形,而前面的方法只对凸多边形有效. 学习了下模拟退火的算法,这个随机算法只在最小圆覆盖的时候写过. 这里再学一下,看起来更正宗一点的.  每次在当前点的附近(R)找是否能优化,而这个R慢慢变小,使得趋紧答案的趋势更精细. 判定点再多边形内:同样,不能用检验是否在每条

hdu 4825 Xor Sum(trie+贪心)

hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #define CLR(a,b) memset((a)

HDU 4952 Poor Mitsui(贪心)

HDU 4957 Poor Mitsui 题目链接 思路:利用相邻交换法去贪心即可,注意容积为0的情况,这是个坑点 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 45; struct SB { int a, b; } sb[N]; bool cmp(SB x, SB y) { return x.b * y.a < x.