HDU 5236 Article [概率DP]

原创 2015年11月19日 13:37:23

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 3
2 2 3
2 1 3
2 1 2
3 2 3 1
2 1 2

3 2 2
3 2 3 1
3 1 2 3

0 0 0
*/


版权声明:文為博主原創文章,未經博主允許不得轉載。

相关文章推荐

hdu5236 Article

题意:你要输入一篇n个字的文章,每到i+0.1s时可以输入一个字符,每到i+0.9s时有p的概率会奔溃,回到开头或者上一个存盘点,每到第i秒有一次机会可以选择按x个健存盘或者不存,打印完整篇文章之后必...

hdu 5236

2015上海邀请赛

hdu - 5236 Article(2015上海大都会赛)概率dp

Article Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

上海邀请赛 A题目 HDU 5236(dp)

先求出没有ctrl+s的时候构造长度为i的期望f[i] 。然后枚举保存的次数,求出最小即可。 #include #include #include #include #include #includ...

hdu 5236 Article 概率dp

题意: 一个人打文章,他需要打n个字符,他在第i+0.1秒的时候输入一个字符,在i+0.9秒的时候系统可能崩溃,那么他将从上次保存的位置开始输入 在每个i时刻他都可以按下x个字符然后按ctrl+s...

【贪心】hdu5236

题意 DRD经常使用一个文本处理软件,这个软件每输入一个字符就有一定的概率(p)崩溃,并且丢失上次保存之后的所有数据。执行一次保存需要x字符的代价(但是不会崩溃)问在最优策略下,输入字符的期望是...

hdu5236 Article(贪心+概率dp)

hdu5236题目(抄别人的http://www.cnblogs.com/qscqesze/p/4543740.html)要求输入一篇N个字符的文章,对所有非负整数i: 每到第i+0.1秒时可以输入...

hdu 5236 Article 概率dp

Article Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5...

hdoj 5240 Exam 【模拟】

Exam Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi...

HDU5236(概率DP,贪心)

题意是敲n个字符,每次敲有p概率崩溃返回上一次的保存状态,保存需要按下x个建。求最小的按键次数的期望。 求出敲n个字母的期望后枚举分成i段,要使得尽量均匀。 #include using nam...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HDU 5236 Article [概率DP]
举报原因:
原因补充:

(最多只允许输入30个字)