zoukankan      html  css  js  c++  java
  • POJ3783Balls[DP 最坏情况最优解]

    Balls
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 907   Accepted: 598

    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 4
    2 14
    3 24
    4 10

    Source

    ------------------------------------------------
    经典问题
    ----------------------------
    f[i][j]表示i层楼j个蛋的最坏情况最优解
    两种可能,碎或不碎f[i][j]=min(f[i][j],max(f[i-1][k-1],f[i][j-k])+1)
    初始化INF,f[i][0]=0,f[1][j]=j 不是j-1
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <cmath>
    using namespace std;
    const int N=1005,B=55,INF=1e9;
    int read(){
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int p,b,m,ans=INF,no;
    int f[B][N];
    void dp(){
        for(int i=0;i<=b;i++) for(int j=1;j<=m;j++) f[i][j]=INF,f[1][j]=j;
        for(int i=1;i<=b;i++)
            for(int j=1;j<=m;j++)
                for(int k=1;k<=j;k++)
                    f[i][j]=min(f[i][j],max(f[i-1][k-1],f[i][j-k])+1);
    }
    int main(int argc, const char * argv[]) {
        p=read();
        for(int i=1;i<=p;i++){
            no=read();b=read();m=read();
            dp();
            ans=f[b][m];
            printf("%d %d
    ",no,ans);
        }
        return 0;
    }
  • 相关阅读:
    HBase Cassandra比较
    重新认识HBase,Cassandra列存储——本质是还是行存储,只是可以动态改变列(每行对应的数据字段)数量而已,当心不是parquet
    HBase底层存储原理——我靠,和cassandra本质上没有区别啊!都是kv 列存储,只是一个是p2p另一个是集中式而已!
    Cassandra 数据模型设计,根据你的查询来制定设计——反范式设计本质:空间换时间
    【LeetCode】【Python解决问题的方法】Best Time to Buy and Sell Stock II
    LVM逻辑卷管理命令
    Java引进和应用的包装类
    Android 4.0新组件:GridLayout详细说明
    【剑指offer】打印单列表从尾部到头部
    原因以及如何避免产生僵尸进程
  • 原文地址:https://www.cnblogs.com/candy99/p/5831703.html
Copyright © 2011-2022 走看看