Hdu 5236 Article【思维+期望Dp】

351 篇文章 2 订阅

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  n characters 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  i strictly 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

题目大意:


每到时间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);
    }
}

















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>