ATM Mechine
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1021 Accepted Submission(s): 515
Problem 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.
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
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
题意:你有一定的存款,数值为0到k不等,每次你可以输入一个取款金额y,如果y大于余额,则会被警告一次,警告次数不能超过w。否则你就能取走这y元,问把存款取完的次数期望。
比赛的时候二分二分搞了好久都没推出来。
首先不管用什么决策(当然是比较优的那种),决策树不会超过十五层。所以w取min(w,15)。
dp[i][j]表示金额上限为i,警告次数为j的取的总次数。
显然我们想要取得次数越少越好,枚举k作为取得金额。
dp[i][j]=min(dp[i][j],dp[i-k][j]+dp[k-1][j-1]+i+1)
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;
const int INF=1000000007;
const int MAXN =2010;
const int MAXM=1000010;
const int MODE=1000000007;
int dp[MAXN][MAXN];
int main(){
for(int i=0;i<MAXN;i++){
for(int j=0;j<MAXN;j++){
dp[i][j]=INF;
}
}
for(int i=0;i<MAXN;i++)
dp[0][i]=0;
for(int i=1;i<MAXN;i++){
for(int j=1;j<16;j++){
for(int k=1;k<=i;k++){
dp[i][j]=min(dp[i][j],dp[i-k][j]+dp[k-1][j-1]+1+i);
}
}
}
int k,w;
while(scanf("%d%d",&k,&w)!=EOF){
w=min(15,w);
double res=(double)dp[k][w]/(k+1);
printf("%.6f\n",res);
}
}