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

    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

    经典的DP问题,问现在有n个鸡蛋,m层楼,需要测试鸡蛋的硬度,如果鸡蛋在第k层没摔坏,但在k+1层摔坏了,鸡蛋的硬度就是k,
    问在最优策略下的最坏情况需要测试多少次,才能测出鸡蛋的硬度?
    设dp[n][m]为n个鸡蛋,m层楼的时候,最优策略的结果。 那么当我们在第i层摔出一个鸡蛋,如果坏掉了,那么问题就转化了需要求,n-1个鸡蛋和i-1层楼,
    也就是dp[n-1][i-1],如果没摔坏就是求dp[n][m-i].

    代码如下:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <algorithm>
    #include <set>
    using namespace std;
    int dp[55][1010];
    #define INF 0x3f3f3f3f
    int solve(int n,int m)
    {
        for(int i=1;i<=m;i++)
         dp[1][i]=i;
        for(int i=1;i<=n;i++)
        dp[i][1]=1;
    
        for(int i=2;i<=n;i++)
             for(int j=2;j<=m;j++)
        {
            dp[i][j]=INF;
            for(int k=1;k<=j;k++)
            dp[i][j]=min(dp[i][j],max(dp[i-1][k-1]+1,dp[i][j-k]+1));
        }
        return dp[n][m];
    }
    int main()
    {
        ios::sync_with_stdio(false);
        int t,id,n,m;
        cin>>t;
        while(t--)
        {
          cin>>id>>n>>m;
          printf("%d %d
    ",id,solve(n,m));
        }
        return 0;
    }
  • 相关阅读:
    搭建Flask+Vue及配置Vue 基础路由
    vue dev 环境下的跨域访问
    vue 调用常量的config.js文件
    PyCharm 通过Github和Git上管理代码
    MongoDB 配置服务
    2018年11月12日
    搭建 flask 应用
    Python 错误总结
    搭建 Django 平台
    go实现主线程等待子线程都运行完再退出
  • 原文地址:https://www.cnblogs.com/a249189046/p/8696051.html
Copyright © 2011-2022 走看看