hdu 5236 Article 概率dp

news/2024/11/24 7:37:35/


Article

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5236

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 0≤T≤20 indicating the number of cases.
Next T lines: each line has a positive integer n≤105, a positive real 0.1≤p≤0.9, and a positive integer x≤100. 

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 10−6.

Sample Input

2 1 0.5 2 2 0.4 2

Sample Output

Case #1: 4.000000
Case #2: 6.444444

HINT

题意

要求输入一篇N个字符的文章,对所有非负整数i:

每到第i+0.1秒时可以输入一个文章字符

每到第i+0.9秒时有P的概率崩溃(回到开头或者上一个存盘点)

每到第i秒有一次机会可以选择按下X个键存盘,或者不存

打印完整篇文章之后必须存盘一次才算完成

输入多组N,P,X选择最佳策略使得输入完整篇文章时候按键的期望最小,输出此期望 

题解:

首先我们先分析不考虑保存的情况的dp

dp[i]=dp[i-1]+p*(1+dp[i])+(1-p);

敲出i个字符, 首先得敲出i-1个字符, 所以有第一部分的dp[i-1]; 然后敲下一个字符时, 有两种可能, p概率会丢失, (1-p)概率不会丢失, 对于丢失的情况就还得重新敲dp[i]次了(期望次数), 不丢失的情况就只有一次就成功了, 所以是(1-p) * 1。

所以化简为: dp[i] = (dp[i-1] + 1) / ( 1- p)

然后我们就考虑保存了,我们枚举保存的次数

然后很显然,我们得均匀的保存,这样子贪心就好了

转自:http://www.cnblogs.com/qscqesze/p/4543740.html

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
const double INF=1e16;
double dp[maxn];
int N,X;
double P;
int main()
{int T,cas=1;scanf("%d",&T);while(T--){scanf("%d%lf%d",&N,&P,&X);memset(dp,0,sizeof(dp));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 a=N/i,b=N%i;ans=min(ans,dp[a+1]*b+dp[a]*(i-b)+X*i);}printf("Case #%d: %.6lf\n",cas++,ans);}return 0;
}



http://www.ppmy.cn/news/165442.html

相关文章

linux学习之防火墙,查看Linux防火墙状态,开启/关闭Linux防火墙,Linux防火墙开放5236端口

Firewalld RHEL7是一个集合多款防火墙管理工具并存的系统&#xff0c;Firewalld动态防火墙管理器服务&#xff08;Dynamic Firewall Manager of Linux systems&#xff09;是目前默认的防火墙管理工具&#xff0c;同时拥有命令行终端和图形化界面的配置工具。相比于传统的防火…

hdu 5236

http://acm.hdu.edu.cn/showproblem.php?pid5236 这是一道概率dp贪心题&#xff1b; 建立状态dp[i]&#xff0c; 表示敲出i个字符的期望次数&#xff0c; 那么有 dp[i] dp[i-1] p*(1 dp[i]) (1-p); 解释一下&#xff1a; 敲出i个字符&#xff0c; 首先得敲出i-1个字符…

HDU 5236 Article

题目来源&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid5236 题意&#xff1a;打印一篇论文需要按键n次&#xff0c;在每i0.1秒时打&#xff0c;在第i0.9秒时有p的概率系统崩溃&#xff0c;崩溃后需要回退到开头或上次保存的地方重新开始。可以选择在第i秒时按下x个…

达梦8之非默认端口(5236)如何实现操作系统认证登录

达梦8之非默认端口(5236)如何实现操作系统认证登录 1、背景 近期遇到诸多金融类项目&#xff0c;在实际生产环境中对达梦SYSDBA默认密码和实例端口&#xff0c;均不允许缺省设置&#xff0c;由此需修改SYSDBA默认密码和默认实例端口号&#xff0c;本文为大家介绍同时修改SYSD…

java中实现对象属性复制的工具类

在 Java 中&#xff0c;有多个工具类可用于实现对象属性的复制&#xff0c;使得属性值从一个对象复制到另一个对象。以下是几个常用的工具类&#xff1a; Apache Commons BeanUtils&#xff1a; Apache Commons BeanUtils 提供了 BeanUtils 类&#xff0c;可以方便地进行属性…

【从零开始进行高精度手眼标定 eye in hand(小白向)1 原理推导】

从零开始进行高精度手眼标定 eye in hand&#xff08;小白向&#xff09;1 原理推导 前言原理推导公式推导为什么在数据采集中至少需要两个位姿信息 MATLAB编程计算A矩阵的计算和获取matlab计算代码B矩阵的计算和获取matlab计算矩阵B 前言 最近由于组内的相关工作需求&#xf…

基于linux的程序库文件打包和调用的实现(二)——动态库文件打包和调用

随着技术的发展&#xff0c;基于linux项目的软件代码越发复杂&#xff0c;原来一个人可以完成的软件项目&#xff0c;现在可能需要多个人合作、多个部门合作、多个企业合作&#xff0c;每个人、每个部门、每个企业可能负责部分软件模块的开发。各个软件模块在调试过程由于涉及企…

从小白到大佬,入门Linux系统收发网络数据包的秘密/

Linux 服务器收到网络数据包&#xff0c;需求经过哪些处置&#xff0c;一步步将数据传给应用进程的呢&#xff1f;应用进程发送数据包时&#xff0c;Linux 又是如何操作将数据包发送进来的呢&#xff1f;今天我们就来聊聊这个话题。 在准备好接纳网络数据包之前&#xff0c;Li…