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

Greater New York Regional 2009

------------------------------------------------

经典问题

----------------------------

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\n",no,ans);
    }
    return 0;
}
时间: 2024-08-25 02:17:29

POJ3783Balls[DP 最坏情况最优解]的相关文章

转:算法的最坏情况与平均情况 复杂度就要看最坏情况

转自:算法的最坏情况与平均情况 如果一个程序运行多次,则有时候它会快点儿,有时候它会慢点儿.算法也一样,在输入1的情况下和输入2的情况下,其执行效率不一定一样.即算法会随着输入数据的不同而有秩序效率的不同,有时候会快点儿,有时候会慢点儿.例如,对一个已经排好序的序列进行排序就要相对容易一些.另外,输入规模的大小也影响算法的运行时间.例如,一个短的序列就比一个很长的序列容易排序. 一般来说,我们希望获得一个算法的时间效率下限,因为所有人都喜欢某种保证:即算法无论如何不会低于我们保证的效率.这种分析

二、给定一个 n 行 m 列的地牢,其中 &#39;.&#39; 表示可以通行的位置,&#39;X&#39; 表示不可通行的障碍,牛牛从 (x0 , y0 ) 位置出发,遍历这个地牢,和一般的游戏所不同的是,他每一步只能按照一些指定的步长遍历地牢,要求每一步都不可以超过地牢的边界,也不能到达障碍上。地牢的出口可能在任意某个可以通行的位置上。牛牛想知道最坏情况下,他需要多少步才可以离开这个地牢。

给定一个 n 行 m 列的地牢,其中 '.' 表示可以通行的位置,'X' 表示不可通行的障碍,牛牛从 (x0 , y0 ) 位置出发,遍历这个地牢,和一般的游戏所不同的是,他每一步只能按照一些指定的步长遍历地牢,要求每一步都不可以超过地牢的边界,也不能到达障碍上.地牢的出口可能在任意某个可以通行的位置上.牛牛想知道最坏情况下,他需要多少步才可以离开这个地牢. 个输入包含 1 个测试用例.每个测试用例的第一行包含两个整数 n 和 m(1 <= n, m <= 50),表示地牢的长和宽.接下来的

最大最小值以及前驱后继操作最坏情况都为O(1)的顺序统计树

问题:通过为结点增加指针的方式,试说明如何在扩张的顺序统计树上,支持每一动态集合查询操作MINIMUM,MAXIMUM,SUCCESSOR和PREDECESSOR在最坏时间O(1)内完成.顺序统计树上的其他操作的渐近性能不应受影响. 代码如下: //本程序在原有的红黑树基础上增加了子树结点个数,前驱后继结点以及最大小结点属性. #include <iostream> #include <time.h> using namespace std; #define BLACK 0 #de

*Algs4-2.1.19希尔排序的最坏情况-(未证明)

2.1.19希尔排序的最坏情况.用1到100构造一个含有100个元素的数组并用希尔排序和递增序列1.4 .13 .40对其排序,使比较的次数尽可能多.非常困难的问题.下面只是目前找到的一个比较次数最多的排列.由于没有严格的数学证明,所以不能算是最后的结果.只是阶段性结果的记录.100,92,84,76,68,60,52,44,36,28,21,14,7,99,91,83,75,67,59,51,43,35,27,20,13,6,98,90,82,74,66,58,50,42,34,26,19,1

最坏情况比较次数

在顺序表中: 顺序查找法:最坏情况下比较n次 查找最大项:最坏情况下比较n-1次 快速排序:    最坏情况下比较n(n-1)/2次 冒泡排序:    最坏情况下比较n(n-1)/2次 堆排序:        最坏情况下比较nlog2n 原文地址:https://www.cnblogs.com/-slz-2/p/11252018.html

最坏情况为线性时间的选择算法

求给定输入中第k大的数的算法. 这是一个常见面试题,通常的解法也很明显,使用类似快排的思想. 每趟运行,把数组的值分成两部分,一部分比pivot大,一部分比pivot小,因为我们知道pivot在数组中的位置,所以比较k和pivot的位置就知道第k大的值在哪个范围,我们不断的进行recursion, 直到pivot就是第k大的值. 这个算法的时间预期是O(n).这里需要注意的是讲的仅限于它的预期,对于这个算法,其在最差情况下,时间复杂度则为n的平法. 参阅快速排序的无敌对手一文,我们是可以构建出一

最坏情况为线性的选择算法

基本思想 主体上是在期望为线性的选择算法上进行改进,将其中的随机的划分元素改为取中位数,使划分更均匀,从而达到最坏时时间复杂度也为线性.需要注意的是实现时里面的索引很晕,别搞混了.我就是先写了个很乱,然后实在改不下去了,就重写了,总共我大概写了5,6个小时吧.(可能我太菜了) 图解 代码 伪代码 这里书中未给伪代码,仅给了整个算法的流程,但并不影响我们的实现 C代码 #include <stdio.h> #define N 50 void show(int *a, int p, int r);

在最坏情况下,找到n个元素中第二小的元素需要n+lgn-2次比较

首先两两比较找到最大的元素,需要n-1次,即二叉树的非叶子节点的个数.之后次最大的一定在和最大的元素比较过的元素中,共有lgn-1个,即树的高度.故加起来就是n+lgn-2 #include<iostream> using namespace std; class Node { public: Node(); Node(int d); Node*left; Node*right; int data; }; Node::Node() { right = left = NULL; } Node::

算法最坏,平均和最佳情况(Worst, Average and Best Cases)-------geeksforgeeks 翻译

最坏,平均和最佳运行时间(Worst, Average and Best Cases) 在上一篇文章中,我们讨论到了渐进分析可以解决分析算法的问题,那么在这一篇中,我们用线性搜索来举例说明一下如何用渐进分析法来分析算法的性能. 我们从三个方面分析算法: 1.最坏情况 2.平均情况 3.最佳情况 这是一段很简单的线性查找的代码 从arr[] 中查找x // Linearly search x in arr[]. If x is present then return the index, // o