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 i−0.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)

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

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

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 2000001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//**************************************************************************************
double f[maxn];
double p,ans;
int n,x;
void init()
{
scanf("%d%lf%d",&n,&p,&x);
}
void solve()
{
init();
for(int i=;i<=n;i++)
f[i]=(f[i-]+)/(-p);
ans=inf;
for(int i=;i<=n;i++)
{
int cnt=n%i;
ans=min(ans,f[n/i+]*cnt+f[n/i]*(i-cnt)+x*i);
}
printf("%.6lf\n",ans);
}
int main()
{
//test;
int t=read();
for(int cas=;cas<=t;cas++)
{
printf("Case #%d: ",cas);
solve();
}
}

最新文章

  1. STM32_RTC君
  2. 插件开发-UI插件开发
  3. Android Studio添加PNG图片报错原因
  4. Hive(二):windows hive ODBC 安装
  5. STL中map与hash_map的比较
  6. 如何获取fragment里的控件
  7. Android如何调用第三方SO库(转)
  8. Java String类和Object类
  9. 关于JS的页面跳转
  10. OpenStack(企业私有云)万里长征第六步——OpenStack网络及虚拟机存储位置
  11. python --- socket模块详解
  12. 重排序、hb、ConcurrentHashMap弱一致性(jdk1.6)
  13. [Hive_add_4] Hive 命令行客户端 Beeline 的使用
  14. Python设计模式 - 基础 - 类/接口之间的六种关系
  15. PyCharm配置Python3开发环境
  16. fastjson的常用方法
  17. css 滚动条样式
  18. 进阶之路(基础篇) - 012 Arduino IDE 添加DHT11传感器第三方库的方法
  19. 北大POJ题库使用指南
  20. a标签 按钮化使用

热门文章

  1. 【Git/GitHub学习笔记】基本操作——创建仓库,本地、远程同步等
  2. dump_stack 实现分析【转】
  3. python 使用headless chrome滚动截图
  4. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 动态树
  5. 2017多校第8场 HDU 6138 Fleet of the Eternal Throne AC自动机或者KMP
  6. Hierarchical Attention Based Semi-supervised Network Representation Learning
  7. 外部div不能包裹内部div的问题
  8. linux命令(34):less命令
  9. linux系统使用过程遇到的bug
  10. AC日记——[SDOI2010]大陆争霸 洛谷 P3690