HDU 5236 Article [概率DP]

来源:互联网 发布:淘宝代绣十字绣可信吗 编辑:程序博客网 时间:2024/06/06 03:43

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  characters in his article. He can press a key to input a character at time , where  is an integer equal or greater than 0. But at every time  for integer  strictly greater than 0, World might crash with probability  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 . Due to the strange keyboard DRD uses, to press Ctrl-S he needs to press  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  indicating the number of cases. 
Next T lines: each line has a positive integer , a positive real , and a positive integer 
 

Output

For each test case: output ''Case #k: ans'' (without quotes), where  is the number of the test cases, and  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 

题意:

题意非常难懂,最后看了别人的表述才明白:你现在要打n个字符,但是程序随时可能会崩溃。
你可以在恰当的时机同时按下X个键用来保存,崩溃后,会从最后一次保存的情况继续开始打字。

解法

可以考虑概率DP,dp(i)表示不保存直接打到i个字符所需要按键数期望
有公式:d(i)=d(i1)+1+pd(i)

化简得:d(i)=11pd(i1)+11p

发现是等比数列,几何倍增长,所以DP前几百个就好了,否则就暴double了。

然后枚举分成几段保存,这几段肯定是尽可能均匀(理由同上),然后直接计算期望就好了。(枚举段的最长长度是不对的,因为一个最长长度可能对应多种分段的方案)

代码:

#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>#include<iostream>#include<stdlib.h>#include<set>#include<map>#include<queue>#include<vector>#include<bitset>#pragma comment(linker, "/STACK:1024000000,1024000000")template <class T>bool scanff(T &ret){ //Faster Input    char c; int sgn; T bit=0.1;    if(c=getchar(),c==EOF) return 0;    while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();    sgn=(c=='-')?-1:1;    ret=(c=='-')?0:(c-'0');    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');    if(c==' '||c=='\n'){ ret*=sgn; return 1; }    while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;    ret*=sgn;    return 1;}#define inf 1073741823#define llinf 4611686018427387903LL#define PI acos(-1.0)#define lth (th<<1)#define rth (th<<1|1)#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)#define mem(x,val) memset(x,val,sizeof(x))#define mkp(a,b) make_pair(a,b)#define findx(x) lower_bound(b+1,b+1+bn,x)-b#define pb(x) push_back(x)using namespace std;typedef long long ll;typedef pair<int,int> pii;double dp[1111];int main(){    tdata{        int n;        double tx;        double p;        scanff(n);scanff(p);scanff(tx);        dp[0]=0.0;        int num=0;        rep(i,1,1000){            num++;            dp[i]=(dp[i-1]+1.0)/(1.0-p);            if(dp[i]>(llinf)/10.0)break;        }        int k=n/num+1;        double ans=llinf*1.0;        rep(i,k,n){            int len=n/i;            int x=n%i;            int y=i-x;            ans=min(ans,double(x)*dp[len+1]+double(y)*dp[len]+double(tx)*double(i));        }        printf("Case #%d: %lf\n",cas,ans);    }}/*3 5 32 2 32 1 32 1 23 2 3 12 1 23 2 23 2 3 13 1 2 30 0 0*/


0 0
原创粉丝点击