URAL1223 Chernobyl’ Eagle on a Roof(dp)

题意:

                有一栋 n n 层楼的建筑,一个鸡蛋在超过x层楼的地方丢下去的时候会碎,现在有 m m 个鸡蛋,问你最坏情况下需要多少次才能知道x的值。
   

思路:

                dp[x][tot]: d p [ x ] [ t o t ] : x x 层楼的时候还有tot个鸡蛋最少的实验次数,那么可以知道:
              dp[x][tot]=min{ max(dp[i1][tot1]+1,dp[xi][tot]+1) | 1ix1 }                             d p [ x ] [ t o t ] = m i n {   m a x ( d p [ i − 1 ] [ t o t − 1 ] + 1 , d p [ x − i ] [ t o t ] + 1 )   |   1 ⩽ i ⩽ x − 1   }
                dp[i1][tot1] d p [ i − 1 ] [ t o t − 1 ] 代表在第 i i 层这个鸡蛋碎了,dp[xi][tot]代表在第 i i 层这个鸡蛋没碎,取最大值是因为要取最坏情况下的值,这样直接考虑的话时间是O(n2m),会超时,但是其实鸡蛋不需要很多就能测出位置了, 1000 1000 层的时候只要 10 10 个鸡蛋就够了,所以 m m 15取个最小值就行了
   

#include<bits/stdc++.h>
typedef long long ll;
const int maxn = 1e3 +10;
const int INF = 1e9 + 10;
using namespace std;

int n, m, T, kase = 1;
int dp[maxn][50];

///dp[x][tot] : 第x层还有tot个鸡蛋的实验最少次数
int dfs(int x, int tot) {
    if(!x) return dp[x][tot] = 0;
    if(!tot) return dp[x][tot] = INF; ///没有鸡蛋了
    if(x == 1) return dp[x][tot] = 1; ///只有一层都还有鸡蛋
    if(dp[x][tot] != -1) return dp[x][tot];
    int &res = dp[x][tot];
    res = dfs(x - 1, tot - 1) + 1; ///第x层丢下去碎了
    for(int i = 1; i < x; i++) {
        ///第i层丢下去没碎
        int si = dfs(x - i, tot) + 1;
        ///第i层丢下去碎了
        int ei = dfs(i - 1, tot - 1) + 1;
        res = min(res, max(si, ei)); ///最差情况
    }
    return res;
}

int main() {
    memset(dp, -1, sizeof dp);
    while(scanf("%d %d", &n, &m) && (n + m)) {
        n = min(15, n);
        int ans = dfs(m, n);
        cout << ans << endl;
    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值