gdcpc2015A題

題目名稱:Article

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5236(在hdu的地址)


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
2 1 0.5 2 2 0.4 2
 

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

思路:dp ,用f[i]表示打出前i個字符所用時的數學期望
         轉移方程爲:f[i] = min (f[j] + g[i-j] + x)
         其中g[i]表示連續打印i個字符不出錯的期望
         所以可以推出g[i] =(g[i-1] + 1) * (1 - p) + (g[i-1] + 1 + g[i]) * p ,前半部分求到i不出錯的,後面求到i出錯的,出錯就得重新開始
         所以g[i] = (g[i-1] + 1) / (1 - p)
 不難發現,g的增長速率非常快,故轉移時只需考察前面很少的狀態

代碼如下:
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=100000+5;
double f[maxn];
int main()
{
    int t,n,x,cnt=0;
    double p;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%lf%d",&n,&p,&x);
        f[0]=0;
        for(int i=1;i<=n;i++)
        {
            double g=1/(1-p);
            f[i]=f[i-1]+g+x;
            for(int j=i-2;j>=0;j--)
            {
                g=(g+1)/(1-p);
                if(g>f[i])
                    break;
                f[i]=min(f[i],f[j]+g+x);
            }
        }
        printf("Case #%d: %.6f\n",++cnt,f[n]);
    }
    return 0;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章