概率DP——HDU 5236

  • 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5236

  • 题意:现在有一个特殊的键盘,在 i+0.1 秒可以输入一个数,在 i+0.9 秒有 p 的概率可能崩溃导致未保存的字符全部丢失,在i秒可以选择保存,每次保存需要按下 x 个键, 求输入N个字符在最优策略的情况下的期望按键次数。

  • 分析: 我们假设输入 i 个字符的期望按键次数为 DP[i] ,那么我们可以发现 DP[i]=DP[i1]+p(DP[i]+1)+(1p) ,转化为 DP[i]=DP[i1]+11p ,再来加上保存的次数,我们枚举保存的次数,然后每次保存间输入的字符数相等(或者相差1),取最小值即可

  • AC代码:

/*************************************************************************
    > File Name: test.cpp
    > Author: Akira 
    > Mail: qaq.febr2.qaq@gmail.com 
 ************************************************************************/

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <set>
#include <list>
#include <ctime>
#include <climits>
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))
using namespace std;

#define MaxN 100001
#define MaxM MaxN*10
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
const int mod = 1E9+7;
const double eps = 1e-6;
#define bug cout<<88888888<<endl;
#define debug(x) cout << #x" = " << x << endl;
int T,n,x;
double p;
double DP[MaxN];
void solve(int t)
{ 
    for(int i=1;i<=n;i++)
    {
        DP[i] = (DP[i-1]+1)/(1-p);
    }
    double ans = INF;
    for(int i=1;i<=n;i++)
    {
        int cnt = n%i;
        ans = min(ans, DP[n/i+1]*cnt+DP[n/i]*(i-cnt)+i*x); 
    }
    printf("Case #%d: %.6lf\n", t, ans);
}
int main()
{
    //std::ios::sync_with_stdio(false);
    scanf("%d", &T);
    for(int t=1;t<=T;t++)
    {
        scanf("%d%lf%d", &n, &p, &x);
        solve(t);
    }
    //system("pause");
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值