Question: there is a number for you to guess. You have k chances and K insurances. If the guess is low, you will be high and low,
If it is too high, it will tell you that it is too high and you lose a K of insurance (k = 0 when it is too high, it will fail). Now you can ask the number range you can guess.
Analysis: DP, two-dimensional dynamic planning. DP is incremented by the insurance K and the guess lead n.
Status: F (G, L) indicates a chance to have a G guess. The value range is 1 ~ N );
Transfer equation: F (G, L) = g (G-1, L) + 1 + f (G-1, L-1) {Guess low + guess high };
Boundary Condition: If there is no chance of failure, you can only guess from 1;
Note ).
#include <iostream>#include <cstdlib>using namespace std;long long F[ 31 ][ 31 ];int main(){ for ( long long i = 0 ; i <= 30 ; ++ i ) F[ i ][ 0 ] = i; for ( int i = 0 ; i <= 30 ; ++ i ) F[ 0 ][ i ] = 0L; for ( int i = 1 ; i <= 30 ; ++ i ) for ( int j = 1 ; j <= 30 ; ++ j ) F[ i ][ j ] = F[ i-1 ][ j ]+F[ i-1 ][ j-1 ]+1; int G,L,C = 1; while ( cin >> G >> L && ( G || L ) ) cout << "Case " << C++ << ": " << F[ G ][ L ] << endl; return 0;}
Zoj 1503-one person "The Price Is Right"