zoukankan      html  css  js  c++  java
  • HDU 5781 ATM Mechine 期望dp

    题目链接:

    http://acm.hdu.edu.cn/showproblem.php?pid=5781

    ATM Mechine

    Time Limit: 6000/3000 MS (Java/Others)
    Memory Limit: 65536/65536 K (Java/Others)
    #### 问题描述 > Alice is going to take all her savings out of the ATM(Automatic Teller Machine). Alice forget how many deposit she has, and this strange ATM doesn't support query deposit. The only information Alice knows about her deposit is the upper bound is K RMB(that means Alice's deposit x is a random integer between 0 and K (inclusively)). > Every time Alice can try to take some money y out of the ATM. if her deposit is not small than y, ATM will give Alice y RMB immediately. But if her deposit is small than y, Alice will receive a warning from the ATM. > If Alice has been warning more then W times, she will be taken away by the police as a thief. > Alice hopes to operate as few times as possible. > As Alice is clever enough, she always take the best strategy. > Please calculate the expectation times that Alice takes all her savings out of the ATM and goes home, and not be taken away by the police. #### 输入 > The input contains multiple test cases. > Each test case contains two numbers K and W. 1≤K,W≤2000 #### 输出 > For each test case output the answer, rounded to 6 decimal places #### 样例 > **sample input** > 1 1 > 4 2 > 20 3 > > **sample output** > 1.000000 > 2.400000 > 4.523810

    题解:

    dp[i][j]表示还剩0到i元,还可以被警告j次的最优期望步数。

    代码

    #include<map>
    #include<queue>
    #include<vector>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define X first
    #define Y second
    #define mkp make_pair
    #define lson (o<<1)
    #define rson ((o<<1)|1)
    #define M (l+(r-l)/2)
    #define bug(a) cout<<#a<<" = "<<a<<endl; 
    
    using namespace std;
    
    typedef __int64 LL;
    
    const int maxn = 2222;
    
    int n, m;
    
    double dp[maxn][22];
    
    int main() {
    	memset(dp, 0x7f, sizeof(dp));
    	printf("%lf
    ",dp[0][0]); 
    	memset(dp[0], 0, sizeof(dp[0]));
    	for (int i = 1; i < maxn; i++) {
    		for (int j = 1; j < 17; j++) {
    			//k表示的是取走k个的情况。
    			//如果剩余的钱数<k,那么你就会用掉一次警告并且你能知道剩余的钱数<k。
    			//如果剩余的钱数>=k,那么你就不会被警告,并且剩余的钱数<i-k。 
    			for (int k = 1; k <= i; k++) {
    				//这边的min体现了选择的是最优策略 
    				dp[i][j] = min(dp[i][j], k*1.0 / (i + 1)*dp[k - 1][j - 1] + (i - k + 1)*1.0 / (i + 1)*dp[i - k][j] + 1);
    			}
    		}
    	}
    	while (scanf("%d%d", &n, &m) == 2 && n) {
    		//m>=15次的时候二分策略就一定能定位了。 
    		m = min(15, m);
    		printf("%.6lf
    ", dp[n][m]);
    	}
    	return 0;
    }
  • 相关阅读:
    剑指Offer 14. 最长公共前缀
    SpringCloud学习笔记【一】:多模块聚合工程基本环境搭建
    SpringCloud学习笔记【零】:官方文档翻译之简介
    SpringBoot利用AOP巧妙记录操作日志
    SpringBoot中BeanValidation数据校验与优雅处理详解
    解决:IDEA无法创建Java文件,只能创建kotlin文件
    SpringBoot实现文件上传功能详解
    SpringBoot快速瘦身,快速部署jar
    SpringBoot的外部化配置最全解析!
    IDEA解决yml配置文件中文输出乱码问题
  • 原文地址:https://www.cnblogs.com/fenice/p/5738219.html
Copyright © 2011-2022 走看看