Hdu 5236 Article【思维+期望Dp】
2017-08-02 19:14
323 查看
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 ncharacters
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 istrictly
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
题目大意:
每到时间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);
}
}
相关文章推荐
- HDU 5236 Article 经典概率dp 由一种状态推到另一种状态
- hdu - 5236 Article(2015上海大都会赛)概率dp
- HDU 5236 Article [概率DP]
- hdu 5236 Article 概率dp
- HDU 5236 Article 期望
- Hdu 5236 Article(dp)
- HDU 5236 Article (概率DP+贪心)
- HDU 5236 Article(概率dp+贪心)
- hdu 5236 Article 概率dp
- hdu 5236 Article 概率dp
- dp 求数学期望 概率 HDU - 5236
- HDU 5074 Hatsune Miku 【dp思维】
- Hdu 4405 Aeroplane chess DP求期望
- 简单概率dp(期望)-hdu-3853-LOOPS
- hdu 5886 Tower Defence 树形期望dp 雪漫防守战
- hdu 4405 概率dp 求期望
- hdu 4118 Holiday's Accommodation 思维+树形dp
- HDU 4405 Aeroplane chess(全期望公式-概率dp)
- HDU 5236 Article
- HDU 2577 How to Type (dp,思维)