ATM Mechine

题目链接:

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

Description


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.

##Input

The input contains multiple test cases.
Each test case contains two numbers K and W.
1≤K,W≤2000

##Output

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

##Source

2016 Multi-University Training Contest 5


##题意:

Alice在ATM取钱,他不知道确切数字而只知道一个上限K,现在他要通过一系列尝试来取出所有钱,每次尝试取y,如果有y则取出,不足y则会被警告,警告超过W次会GG.
Alice会尽量用少的次数去尝试,求尝试次数的期望.


##题解:

概率DP.
dp[i][j]:存款上限为i,还能被警告不超过j次时的次数期望.
由于每次取的数是等概率的:每次取k∈[0,i],其中[0,k-1]这k个数会导致这次取钱被警告.
所以不被警告的期望是 (i+1-k)/(i+1) * dp[i-k][j];
被警告的期望是 k/(i+1) * dp[k-1][j-1]; (由于取k失败,那么剩余上限为k-1)
转移方程:![](http://images2015.cnblogs.com/blog/764119/201608/764119-20160803171120184-793251686.png)
关键点:由于Alice会用尽量少的次数去尝试,那么他尝试的次数不会超过二分的次数:![](http://images2015.cnblogs.com/blog/764119/201608/764119-20160803171146028-149464081.png)
所以j的范围是min(15,W), 这样才能使得dp不超时.

采用记忆化搜索的形式来求DP值.
对于所有数据DP数组的值是共用的,所以只用初始化一次.(否则会TLE).
1007(HDU5787)的DP也是如此,切记.


##代码:

``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define mid(a,b) ((a+b)>>1)
#define eps 1e-8
#define maxn 2100
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

double dp[maxn][maxn];

double get_dp(int i, int j) {

if(i == 0) return dp[i][j] = 0;

if(j == 0) return dp[i][j] = inf;

if(dp[i][j] != -1.0) return dp[i][j];

double ans = inf;

for(int k=1; k<=i; k++) {

ans = min(ans, (i+1-k)/(i+1.0)get_dp(i-k,j) + k/(i+1.0)get_dp(k-1,j-1) + 1);

}

return dp[i][j] = ans;

}

int main(int argc, char const *argv[])

{

//IN;

int k,w;
for(int i=0; i<maxn; i++)
for(int j=0; j<maxn; j++)
dp[i][j] = -1.0;
while(scanf("%d %d", &k,&w) != EOF)
{
printf("%.6f\n", get_dp(k,min(w,15)));
} return 0;

}

</big>

HDU 5781 ATM Mechine (概率DP)的更多相关文章

  1. HDU 5781 ATM Mechine 期望dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 ATM Mechine Time Limit: 6000/3000 MS (Java/Othe ...

  2. hdu 5781 ATM Mechine dp

    ATM Mechine 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 Description Alice is going to take ...

  3. HDU 5781 ATM Mechine

    题目大意:某个未知整数x等概率的分布在[0,k]中.每次你都可以从这个整数中减去一个任意整数y,如果x>=y,那么x=x-y,操作次数累计加1:否则,将会受到一次错误提示.当错误提示超过w次,将 ...

  4. ATM Mechine (概率DP)

    题意:去银行取最多K钱,想要全部取完,但是有个限制就是如果你输入取钱的额度超过了你已有的钱,那么会接受一次警告并无法取钱,然后求最多不超过w次警告的前提下你取完所有钱所需要的最少次数. 思路:概率DP ...

  5. 【动态规划】HDU 5781 ATM Mechine

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 题目大意: 一个人有[0,K]内随机的钱,每次可以随意取,但是不知道什么时候取完,取钱超过剩余 ...

  6. HDU 4089 Activation(概率DP)(转)

    11年北京现场赛的题目.概率DP. 公式化简起来比较困难....而且就算结果做出来了,没有考虑特殊情况照样会WA到死的.... 去参加区域赛一定要考虑到各种情况.   像概率dp,公式推出来就很容易写 ...

  7. HDU 4405 Aeroplane chess (概率DP)

    题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i  这个位置到达 n ...

  8. HDU - 5001 Walk(概率dp+记忆化搜索)

    Walk I used to think I could be anything, but now I know that I couldn't do anything. So I started t ...

  9. HDU 2955 Robberies 背包概率DP

    A - Robberies Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submi ...

随机推荐

  1. Android 下log的使用总结

    Android 下log的使用总结 一:在源码开发模式下 1:包含头文件: #include <cutils/log.h> 2:定义宏LOG_TAG #define LOG_TAG &qu ...

  2. ruby安装插件报错

    有时候我们需要安装ruby插件的时候,会报如下错误:  ERROR: Could not find a valid gem 'rdiscount' (>= 0), here is why: Un ...

  3. Codeforces Round #207 (Div. 2)C

    读错题意了..线段树延迟标记 白刷这么多线段树 #include <iostream> #include<cstdio> #include<cstring> #in ...

  4. Android系列之Fragment(一)----Fragment加载到Activity当中

    Android上 的界面展示都是通过Activity实现的,Activity实在是太常用了.但是Activity也有它的局限性,同样的界面在手机上显示可能很好看, 在平板上就未必了,因为平板的屏幕非常 ...

  5. BZOJ2299: [HAOI2011]向量

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2299 题解:乱搞就可以了... 不妨认为有用的只有(a,b)(a,-b)(b,a)(b,-a) ...

  6. BZOJ2253: [2010 Beijing wc]纸箱堆叠

    题解: 其实就是求三维偏序最长链.类似于三维逆序对,我们可以用树状数组套平衡树来实现. DP方程 :f[i]=max(f[j]+1) a[j]<a[i] 我们按一维排序,另一位建立树状数组,把第 ...

  7. ActionBarSherlock的学习笔记(二) ------------ 创建ActionBarSherlock

    将ActionBarSherlock 作为库项目添加到当前的项目中去,然后创建ActionBar,并自定义ActionBar的标题栏 例子如下: import android.os.Bundle; i ...

  8. 【转】statfs获得硬盘使用情况 模拟linux命令 df

    原文网址:http://blog.csdn.net/mociml/article/details/5335474 说明:本文以主要为转载内容,同时加入了我在使用过程中遇到问题对其的修正!!!!!!!! ...

  9. Top Android App使用的组件 3

    8684公交 AdChina:com.adchina:易传媒广告平台 AdsMogo:com.adsmogo:芒果移动广告平台 大姨吗 AChartEngine:org.achartengine:An ...

  10. [Papers]NSE, $\pi$, Lorentz space [Suzuki, NA, 2012]

    $$\bex \sen{\pi}_{L^{s,\infty}(0,T;L^{q,\infty}(\bbR^3))} \leq \ve_*, \eex$$ with $$\bex \frac{2}{s} ...