码迷,mamicode.com
首页 > 其他好文 > 详细

UVA1627-Team them up!(动态规划)

时间:2018-12-08 20:19:14      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:data   color   动态   sed   ros   return   一个   分享图片   from   

Problem UVA1627-Team them up!

Total Submissions:3577  Solved:648

Time Limit: 3000 mSec

技术分享图片 Problem Description

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 ?lled up a large crate of identical water 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 e?ort as possible hauling their balloons up the stairs, so they would like to know the lowest ?oor from which they can drop the balloons so that they do burst. You know the building has n ?oors, and your friends have given you k identical balloons which you may use (and break) during your trials to ?nd 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 ?oor 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 ?oor). A trial consists of dropping a balloon from a certain ?oor. 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

 

题解:这个题属于比较经典的动态规划题,dp(i,j)表示i个水球,做j次实验能够测出的最高楼层,考虑第一个水球,将其在第k层扔下,如果炸了,那就说明硬度<k,为了能够在剩下的i-1个水球和j-1次实验机会中测出硬度,必须要求k-1<=dp(i-1,j-1),也就是说k最大dp(i-1,j-1)+1,如果没炸,那么k+1层相当于新的1层,还有i个球,j-1次机会,能够测到的楼层自然是dp(i-1,j),因此加上前面的,就是总共能测得最高的。

 

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 typedef long long LL;
 6 
 7 const int maxn = 100 + 5;
 8 
 9 int k;
10 LL n, dp[maxn][65];
11 
12 LL DP(int k, int i) {
13     if (dp[k][i] > 0) return dp[k][i];
14     if (!k || !i) return dp[k][i] = 0;
15 
16     return dp[k][i] = DP(k - 1, i - 1) + DP(k, i - 1) + 1;
17 }
18 
19 int main()
20 {
21     //freopen("input.txt", "r", stdin);
22     memset(dp, -1, sizeof(dp));
23     while (~scanf("%d%lld", &k, &n) && k) {
24         int i;
25         for (i = 0; i <= 63; i++) {
26             if (DP(k, i) >= n) {
27                 printf("%d\n", i);
28                 break;
29             }
30         }
31         if (i == 64) printf("More than 63 trials needed.\n");
32     }
33     return 0;
34 }

 

UVA1627-Team them up!(动态规划)

标签:data   color   动态   sed   ros   return   一个   分享图片   from   

原文地址:https://www.cnblogs.com/npugen/p/10088772.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!