UVa 10934 Dropping water balloons
2015-06-03 16:46
441 查看
Problem A: Dropping water balloons
It's frosh week, and this year your friends have decided that they would initiate the new computer science students by dropping water balloons on them. They've filled up a large crate of identicalwater balloons, 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 bursting!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 find their answer. 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 droppedfrom 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.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 fits intoa 64 bit integer (yes, it's a very tall building). The last case has k=0 and should not be processed.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. insteadof the number.Sample input
2 100 10 786599 4 786599 60 1844674407370955161 63 9223372036854775807 0 0
Output for sample input
14 21 More than 63 trials needed. 61 63
#include <cstdio>#include <cstring>#include <iostream>using namespace std;// record[i][j]代表用i个气球尝试j次所能测出的楼层最高高度long long record[110][65];int main(){// 算出所有答案record[0][0] = 0;record[1][0] = 0;// memset(record, 0, sizeof(record));for(int i = 1; i <= 100; i++){for(int j = 1; j <= 63; j++){record[i][j] = record[i-1][j-1] + 1 + record[i][j-1];}}long long k, n;// while(scanf("%lld %lld", &k, &n) == 2 && !(k == 0 && n == 0))while(cin >> k >> n && k){int i;for(i = 1; i <= 63; i++){if(record[k][i] >= n){printf("%d\n", i);break;}}if(i > 63)printf("More than 63 trials needed.\n");}return 0;}
该题目没有想出来,因为楼高太高,没法用数组来表示。
书上的做法是定义d(i,j)为用i个气球,尝试j次得到答案,所能测试的楼的最大高度。
假设一开始在第k层楼扔第一个气球,为了保证能在j次能得到答案。
如果气球破了,那么需要在1至(k-1)层用i-1个气球,尝试j-1得到答案。
k最大就为d(i-1,j-1)+1.
如果气球没破,那么在第k层楼之上的最大高度就为用i个气球,尝试j-1次得到答案
综上,最终的楼高应为d(i,j) = d(i-1,j-1) + 1 + d(i, j-1).
注意点是:当发现某个量不能表示进状态时(数值过大),考虑别的角度。
相关文章推荐
- Linux netstat命令详解
- heartbeat(v2)实现LAMP提供wordpress博客站点高可用模型实践
- 在Mac系统上安装Tomcat
- apache 配置虚拟主机。
- previous operation has not finished
- 使用Nginx提供web服务
- zabbix监控percona容器
- centos下安装iftop
- centos下安装iftop
- docker构建percona容器
- linux安装mysql 源码安装mysql
- CentOS中设置.so链接文件路径
- (总结)Nginx配置文件nginx.conf中文详解
- SharePoint 2010 中创建超链接到Pop-Up对话框
- eclipse下的tomcat内存设置大小
- linux/unix安装mysql 二进制包安装
- Hadoop2.x Yarn作业提交(客户端)
- CopyOnWriteArrayList 解读
- CopyOnWriteArrayList 解读
- CS架构和BS架构的区别