zoukankan      html  css  js  c++  java
  • hiho一下 第206周

    题目1 : Guess Number with Lower or Higher Hints

    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    There is a game about guessing number between 1 and N. This game is played by two people A and B. When A guesses a number(e.g. 5), he needs to pay that amount of dollars($5). Then B will give a hint whether A's guess is lower, higher or correct. B will choose the hint that forces to gain maximal amount of earning as long as it's not conlict with previous hints.

    Assume they are both smart enough. What is the minimal amount that A needs to pay to finish this game? That is, there is only one number left and A will surely guess it right. Careful: B does not have a fixed number in mind.  

    输入

    An integer N. (1 <= N <= 200)

    输出

    The minimal amount A needs to pay.

    样例输入
    5
    样例输出
    6

    题目意思:

      A和B玩猜数字的游戏,范围是1到N,B心中想一个数,然后A猜一个数k,付出的费用就是 k,这时候B会告诉A,猜的数是高了,低了,还是猜对了,并且B每次的提示都要和前面所有的提示不冲突。B要使得尽可能的让A多出钱,而A要尽可能的少出钱,求A猜对B心中想的数要付出的最小代价。

    思路:

      看懂大概题意就直接想到了二分,这种猜数字的题一般就是二分猜的次数是最少的,然后果断写了二分,wa了一次。后面再仔细看了下题目,二分的时候是只站在A考虑的,B想的数字是什么并没有考虑,然后越想越觉得是一个区间DP的题,果断写了一个区间DP,AC~~

      具体区间DP思路:我定义dp[i][j]为从i到j这个区间中猜中j所需要花费的最小代价,假设我在ij之间猜了一个数k,那么我猜到j的最小代价就是   max(i到k-1的最小代价, k+1到j的最小代价)+k。

      AC代码:

    // Asimple
    #include <bits/stdc++.h>
    #define debug(a) cout<<#a<<" = "<<a<<endl
    #define sysp system("pause")
    using namespace std;
    typedef long long ll;
    const int maxn = 200 + 5;
    const int INF = 1e9+5;
    ll T, n, sum, num, m, t, len, k;
    ll dp[maxn][maxn];
    
    void input() {
        while( cin >> n ) {
            for(int l = n; l>0; l -- ) {
                for(int r = l + 1; r <= n; r ++) {
                    dp[l][r] = INF;
                    for(int k=l; k<=r; k++) {
                        dp[l][r] = min(dp[l][r], max(dp[l][k-1], dp[k+1][r])+k);
                    }
                }
            }
    
            cout << dp[1][n] << endl;
        }
        // sysp;
    }
     
    int main() {
        input();
        return 0;
    }
  • 相关阅读:
    第六课 变量和常量
    第六课 实型(浮点型)
    第五课 字符型数据 (主函数冲突)
    第四课 整型数据(主函数冲突)
    第二课(需订正)运行失败解决方法
    第三课 数据类型分类(关键字)
    第二课
    第一课
    贝叶斯滤波与卡尔曼滤波第九讲笔记《粒子滤波》
    贝叶斯滤波与卡尔曼滤波第八讲代码
  • 原文地址:https://www.cnblogs.com/Asimple/p/9186764.html
Copyright © 2011-2022 走看看