Uva10934_经典的动态规划习题

动态规划训练4——Uva10934

                   Dropping water balloons
It’s frosh week, and this year your friends have decided that they would initiate the new computer science students by drop-ping water balloons on them. They’ve pulled up a large crate of identical waterballoons, ready for the event. But as fate would have it, the balloons turned out to be rather tough, and can be dropped from a height of several stories without burst-ing!
So your friends have sought you out for help. They plan to drop the balloons from a tall building on campus, but would like to spend as little effort as possible hauling their balloons up the stairs, so they would like to know the lowest floor from which they can drop the balloons so that they do burst.
You know the building has n floors,and your friends have given you k identical balloons which you may use (and break) during your trials to nd their an-swer. Since you are also lazy, you would like to determine the minimum number of trials you must conduct in order to determine with absolute certainty the lowest floor from which you can drop a balloon so that it bursts (or in the worst case, that the balloons will not burst even when
dropped from the top floor). A trial consists of dropping a balloon from a certain floor. If a balloon fails to burst for a trial, you can fetch it and use it again for another trial.
Input
The input consists of a number of test cases, one case per line. The data for one test case consists of two numbers k and n, 1 k 100 and a positive n that ts into a 64 bit integer (yes, it’s a very tall building). The last case has k = 0 and should not be processed.
Output
For each case of the input, print one line of output giving the minimum number of trials needed to solve the problem. If more than 63 trials are needed then print `More than 63 trials needed.’ instead of the number.
Sample Input
2 100
10 786599
4 786599
60 1844674407370955161
63 9223372036854775807
0 0
Sample Output
14
21
More than 63 trials needed.
61
63

  本题是动态规划中的一经典例题。初看题目很容易直接设置状态(楼层,气球数),并且也能较为容易的找到转移方程。但是分析题目后容易发现楼层大小n关于试验数trial和气球数k成指数增长,复杂度较高,且题目中n的范围为64bit_int,远超出stack范围,所以显然不能作为状态量。因此我们考虑将trial和k作为状态量:即考虑当trial和k已知时,能测试的最大楼层数n。
  转移方程的书写见紫书p292:例题9-20。
  此题转移方程书写时的思想值得借鉴,同时本题中“转换待求量”以更好地设置状态、进行转移的方法也十分值得学习!!

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <math.h>
long long dp[105][70];
int main(void) {
	for (int i = 1; i <= 100; ++i)
		for (int j = 1; j <= 63; ++j)
			dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1] + 1;
	int k; long long n;
	while (scanf("%d%lld", &k, &n) != EOF && k) {
		int i;
		for (i = 1; i <= 63; ++i)
			if (dp[k][i] >= n) break;
		if (i == 64) printf("More than 63 trials needed.\n");
		else printf("%d\n", i);
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值