zoukankan      html  css  js  c++  java
  • Balls(poj 3783)

    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 4

    2 14

    3 24

    4 10


    题目大意:
    有一些鸡蛋,我们现在想知道这些鸡蛋的硬度。然后现在有一座很高很高的大楼里,我们现在要在这座大楼上测试鸡蛋的硬度。每个鸡蛋的硬度相同,鸡蛋的硬度定义为:如果鸡蛋从第 m
    层上掉下来没有破裂,而从第 m+1 层上掉下来就破裂了,那么这个鸡蛋的硬度就是 m 。某个鸡蛋如果在实验中破裂了就永远的损失了。我们现在有 n

    个鸡蛋。那么在最坏情况下我们最少需要做多少次实验呢?

    输入数据:是 T 组数据,然后第一个数 是标号 op
    ,然后输入两个整数 M,和 N,分别表示有 M 个鸡蛋和 N层楼。
    输出数据:标号 op , 和最坏情况下我们最少需要做多少次实验 ans

    解题思路:
    这是一个比较经典的 DP
    问题,又叫做 “扔鸡蛋问题”,假设 dp[n,m] 表示 n 层楼、m 个鸡蛋时找到摔鸡蛋不碎的最少判断次数。则一个鸡蛋从第 i 层扔下,如果碎了,还剩 m−1 个鸡蛋,为确定下面楼层中的安全位置,还需要dp[i−1,m−1] 次(子问题);不碎的话,上面还有 n−i 层,还需要 dp[n−i,m]次(子问题,实体 n 层楼的上 n−i 层需要的最少判断次数和实体 n−i 层楼需要的最少判断次数其实是一样的)。

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stack>
     4 #include<queue>
     5 #include<iostream>
     6 #include<algorithm>
     7 #include<map>
     8 #include<vector>
     9 #define PI acos(-1.0)
    10 using namespace std;
    11 typedef long long ll;
    12 const int Inf=0x3f3f3f3f;
    13 int m,n,k,p;
    14 int str[100000];
    15 int ans[100001];
    16 int dp[1000][1000];////dp[i][j]:表示在 i 层楼 还有 j 个鸡蛋的最小判断次数
    17 int di[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
    18 map<ll,ll>::iterator it;
    19 void solve(int p,int k)
    20 {
    21     memset(dp,0,sizeof(dp));
    22     for(int i=1;i<=k;i++)
    23     {
    24         dp[i][1]=i;//只有一个鸡蛋的情况
    25     }
    26     for(int i=1;i<=p;i++)
    27     {
    28         dp[1][i]=1;//只有一层楼的情况
    29     }
    30     for(int i=1;i<=k;i++)
    31        for(int j=2;j<=p;j++)
    32         {
    33             dp[i][j]=Inf;
    34             for(int t=1;t<=i;t++)
    35             {
    36                 dp[i][j]=min(dp[i][j],max(dp[t-1][j-1],dp[i-t][j])+1);
    37             }
    38         }
    39         cout<<n<<" "<<dp[k][p]<<endl;
    40 }
    41 int main()
    42 {
    43    cin>>m;
    44    while(m--)
    45    {
    46        cin>>n>>p>>k;
    47        solve(p,k);
    48    }
    49 }
  • 相关阅读:
    Android开发日记(三)
    Android开发日记(二)
    Bundle savedInstanceState的作用
    Android Bundle类
    Consumer
    饭卡
    《CLR via C#》读书笔记 之 泛型
    WCF寄宿到Windows Service
    WCF中配置文件解析
    WCF Service Configuration Editor的使用
  • 原文地址:https://www.cnblogs.com/moomcake/p/9798946.html
Copyright © 2011-2022 走看看