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
*/


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

相关文章推荐

Hdu 5236 Article(dp)

题目链接 Article Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)...

【DP】 HDOJ 5236 Article

令dp[i]代表打出i个字符所需的期望,res[i]代表一口气打出i个字符的期望。对于res的转移有res[i] = (res[i-1]+1) * (1-p) + (res[i]+res[i-1]+1...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

HDU 5236 Article

Article Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.p...

hdu 3507 Print Article 斜率优化dp

hdu 3507 Print Article 斜率优化

斜率优化dp简讲 && HDU 3507 Print Article

Problem DescriptionZero has an old printer that doesn’t work well sometimes. As it is antique, he st...

HDU 3507:Print Article(斜率DP)

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) T...

Print Article HDU 3507(斜率DP入门模板题)

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

dp斜率优化 Hdu 3507(Print Article)详细题解

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3507累加器传送门:http://blog.csdn.net/noiau/article/detail...
  • NOIAu
  • NOIAu
  • 2017-05-13 08:26
  • 171

hdu 3507 Print Article 斜率优化dp

hdu 3507 Print Article http://acm.hdu.edu.cn/showproblem.php?pid=3507 从http://www.cnblogs.com/ka20...

斜率优化DP模板题--HDU3507 Print Article

题目大意:给出一串正数,将这一串数分成若干段,每一段的代价为这一段数的加和的平方+一个常数m,求最小代价。不难写出DP转移方程:f[i]=max(f[j]+(sum[i]−sum[j])2+m)f[i...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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