HDU 5236 Article(概率dp+贪心)
2016-05-10 18:07
267 查看
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 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
首先dp[i]表示敲i个字符不崩溃的概率
dp[i]=(dp[i-1]+1)*(1-p)+(dp[i-1]+1+dp[i])*p
第一部分表示第i个字符没有崩溃,第二部分表示第i个字符崩溃,那还要敲i个字符
然后贪心枚举分成多少段
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
首先dp[i]表示敲i个字符不崩溃的概率
dp[i]=(dp[i-1]+1)*(1-p)+(dp[i-1]+1+dp[i])*p
第一部分表示第i个字符没有崩溃,第二部分表示第i个字符崩溃,那还要敲i个字符
然后贪心枚举分成多少段
#include <iostream> #include <cstring> #include <cmath> #include <algorithm> #include <cstdio> #include <vector> #include <queue> #include <set> #include <map> #include <stack> #define LL long long #define INF 0x3f3f3f3f #define MOD 29393 using namespace std; const int maxn = 1e5+100; double dp[maxn]; int main() { int T, n, x; double p; scanf("%d",&T); for(int cas=1; cas<=T; cas++) { scanf("%d %lf %d",&n, &p, &x); 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 d = n / i; int m = n % i; double ans1 = dp[d+1] * m + dp[d] * (i-m); double ans2 = x * i; ans = min(ans, ans1+ans2); } printf("Case #%d: %.6lf\n",cas,ans); } return 0; }
相关文章推荐
- 这些道理没人告诉你读书笔记
- matlab 源码阅读
- win10快捷键大全
- android 获取路径目录方法以及判断目录是否存在,创建目录
- android中include标签的使用
- ionic开发插件之ngCordova配置安装(搬运)
- oracle 查询表的大小,表空间的使用情况,默认表空间
- OneZero第七周第一次站立会议(2016.5.9)
- 慢请求调查方法(先占个坑)
- Android事件传递机制
- ip default-network 用法举例(使用静态默认路由实现网络互连)
- SimpleAdapter使用案例
- WDF DMA一些概念(2)
- [caffe]thread
- c++第五次作业
- WMware 10 Ubuntu 12.04 进入Unity模式
- 不安装oracle客户端,用plsql连接oracle
- 对kvm虚拟机恢复有用知识
- eclipse迁移到Android Studio 问题记录
- 必须掌握的八种排序(1-2)--插入排序,希尔排序