藍橋杯-摔手機問題【dp】

來源:互聯網
上載者:User

標籤:sum   sed   分享   like   stream   you   his   view   min   

非常詳細的題解:戳這裡

例題:poj-3783 Balls

Balls
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1151   Accepted: 748

Description

The classic Two Glass Balls brain-teaser is often posed as: 

"Given two identical glass spheres, you would like to determine the lowest floor in a 100-story building from which they will break when dropped. Assume the spheres are undamaged when dropped below this point. What is the strategy that will minimize the worst-case scenario for number of drops?"


Suppose that we had only one ball. We‘d have to drop from each floor from 1 to 100 in sequence, requiring 100 drops in the worst case. 

Now consider the case where we have two balls. Suppose we drop the first ball from floor n. If it breaks we‘re in the case where we have one ball remaining and we need to drop from floors 1 to n-1 in sequence, yielding n drops in the worst case (the first ball is dropped once, the second at most n-1 times). However, if it does not break when dropped from floor n, we have reduced the problem to dropping from floors n+1 to 100. In either case we must keep in mind that we‘ve already used one drop. So the minimum number of drops, in the worst case, is the minimum over all n. 

You will write a program to determine the minimum number of drops required, in the worst case, given B balls and an M-story building.

Input

The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set consists of a single line containing three(3) decimal integer values: the problem number, followed by a space, followed by the number of balls B, (1 ≤ B ≤ 50), followed by a space and the number of floors in the building M, (1 ≤ M ≤ 1000).

Output

For each data set, generate one line of output with the following values: The data set number as a decimal integer, a space, and the minimum number of drops needed for the corresponding values of B and M.

Sample Input

4 1 2 10 2 2 100 3 2 300 4 25 900

Sample Output

1 42 143 244 10

Source

Greater New York Regional 2009

附ac代碼:

dp法一:

 1 #include<iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 #include <string> 7 typedef long long ll; 8 using namespace std; 9 const int maxn = 1e3 + 10;10 const int inf = 0x3f3f3f3f;11 int a[maxn], b[maxn], c[maxn];12 int dp[111][maxn];13 int main()14 {15     int n, m;16   //  scanf("%d %d", &n, &m);17     for(int j = 1; j <= 1005; ++j)18     {19         dp[1][j] = j;20     }21     for(int i = 1; i <= 55; ++i)22     {23         dp[i][1] = 1;24     }25     for(int i = 2; i <= 55; ++i)26     {27         for(int j = 1; j <= 1005; ++j)28         {29             dp[i][j] = inf;30             for(int k = 1; k <= j; ++k)31             dp[i][j] = min(dp[i][j],max(dp[i - 1][k - 1], dp[i][j - k]) + 1);32         }33     }34   //  int ans = 111;35     int t;36     int cas;37     scanf("%d", &t);38     while(t--) {39         scanf("%d %d %d", &cas, &n, &m);40         printf("%d %d\n", cas, dp[n][m]);41     }42     43     return 0;44 }
View Code

dp法二:

藍橋杯-摔手機問題【dp】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.