[经典DP] UVa10934 装满水的气球

题目

这里写图片描述

思路

常规思路应该是d(i,j),用i个气球有j层楼,最少用多少次测出。
但本题的样例是这样的:

Sample Input
2 100
10 786599
4 786599
60 1844674407370955161
63 9223372036854775807
0 0
Sample Output
14
21
More than 63 trials needed.
61
63

这j这么大。。。哪能装到数组里去。。
所以本题应该转变思路,这也是本题称作经典DP的原因,下面是正确思路
(大中午的一直看不懂。。哪天清醒了好好研究研究)
这里写图片描述

代码

注意long long,要是怕不保险甚至可以上unsigned long long

#include<iostream>
#include<cstring>
using namespace std;

const int maxk = 100;
const int maxa = 63;

long long d[maxk + 1][maxa + 1];

int main() {
    memset(d, 0, sizeof(d));
    for (int i = 1; i <= maxk; i++)
        for (int j = 1; j <= maxa; j++)
            d[i][j] = d[i - 1][j - 1] + 1 + d[i][j - 1];

    int k;
    long long n;
    while (cin >> k >> n && k) {
        int ans = -1;
        for (int i = 1; i <= maxa; i++)
            if (d[k][i] >= n) { ans = i; break; }
        if (ans < 0) cout << "More than " << maxa << " trials needed.\n";
        else cout << ans << "\n";
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/icecab/article/details/81043478
今日推荐