Hdu 5236 Article【思维+期望Dp】

来源:互联网 发布:stevie nicks 知乎 编辑:程序博客网 时间:2024/06/06 08:56

Article

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 978    Accepted Submission(s): 360


Problem Description
As the term is going to end, DRD begins to write his final article.

DRD uses the famous Macrohard's software, World, to write his article. Unfortunately this software is rather unstable, and it always crashes. DRD needs to write ncharacters in his article. He can press a key to input a character at time i+0.1, where i is an integer equal or greater than 0. But at every time i0.1 for integer istrictly greater than 0, World might crash with probability p and DRD loses his work, so he maybe has to restart from his latest saved article. To prevent write it again and again, DRD can press Ctrl-S to save his document at time i. Due to the strange keyboard DRD uses, to press Ctrl-S he needs to press x characters. If DRD has input his total article, he has to press Ctrl-S to save the document. 

Since World crashes too often, now he is asking his friend ATM for the optimal strategy to input his article. A strategy is measured by its expectation keys DRD needs to press. 

Note that DRD can press a key at fast enough speed. 
 

Input
First line: an positive integer 0T20 indicating the number of cases.
Next T lines: each line has a positive integer n105, a positive real 0.1p0.9, and a positive integer x100.
 

Output
For each test case: output ''Case #k: ans'' (without quotes), where k is the number of the test cases, and ans is the expectation of keys of the optimal strategy.
Your answer is considered correct if and only if the absolute error or the relative error is smaller than 106
 

Sample Input
21 0.5 22 0.4 2
 

Sample Output
Case #1: 4.000000Case #2: 6.444444

题目大意:


每到时间i+0.1的时候,可以打一个字。

时间到i+0.9的时候,程序可能有P的概率崩掉,再打开软件的时候,会回到上一次保存的情况。

我们在时间i的时候,可以花费X次按键来保存这份文章。

问打完一个包含N个字的文章期望的最小按键次数。


思路:


我们首先设定Dp【i】,表示打出i个字的期望。

那么我们首先不考虑回归上一次的情况,因为界定出来的话时间复杂度会达到O(n^2),我们不妨先把这一点抛开外。


那么有:Dp【i】=Dp【i-1】+1+p*Dp【i】;

那么化简有:Dp【i】=(Dp【i-1】+1)/(1-p);


贪心的想,我们肯定是周期性的去保存是最优的,所以我们接下来我们考虑枚举打多少字之后保存一次,去维护最小值即可。


Ac代码:

#include<stdio.h>#include<string.h>#include<iostream>using namespace std;double dp[150000];int main(){    int kase=0;    int t;    scanf("%d",&t);    while(t--)    {        int n,x;        double p;        scanf("%d%lf%d",&n,&p,&x);        for(int i=1;i<=n;i++)dp[i]=(dp[i-1]+1)/(1-p);        double ans=10000000000;        for(int i=1;i<=n;i++)        {            int ci=n/i;            int yu=n%i;            ans=min(ans,dp[ci+1]*yu+dp[ci]*(i-yu)+i*x);        }        printf("Case #%d: ",++kase);        printf("%lf\n",ans);    }}