Hdu 5236 Article(dp)

news/2024/11/24 2:09:37/

题目链接

Article

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 404    Accepted Submission(s): 119


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 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 0T20 indicating the number of cases.
Next T lines: each line has a positive integer n105 , a positive real 0.1p0.9 , and a positive integer x100 .

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 106 .

Sample Input
  
2 1 0.5 2 2 0.4 2

Sample Output
  
Case #1: 4.000000 Case #2: 6.444444

Source
The 2015 ACM-ICPC China Shanghai Metropolitan Programming Contest

题意:要敲n个字符的文本,每按一个键以后,可以选择是否保存当前文本,若要保存需要按x个键(不会崩溃)。然后有p的概率文本崩溃,崩溃以后文本回到最近保存的状态。有(1-p)的概率没有崩溃。。然后继续输入下一个字符。求输入完整个文本最少按键的期望次数?

题解:用f[i]表示一次不保存并输入长度为i的文本的按键的期望次数。则:

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

f[i]=(f[i-1]+1)/(1-p);

接下来我们有两种方法。

方法一:用dp[i]表示输入长度为i的文本且一定保存好的最少期望按键次数。那么:

dp[i]=min(dp[i],dp[k]+f[i-k]+x);

用w(i,j)表示f[i-j],可以证明:

w(i,j)+w(i+1,j+1)<=w(i+1,j)+w(i,j+1);

即 我们要证明:2*f[x]<=f[x-1]+f[x+1];

因为:

f[x-1]=(1-p)*f[x]-1;

f[x+1]=(f[x]+1)/(1-p);

所以我们需要证明:2*f[x]<=(1-p)*f[x]-1+f[x]/(1-p)+1/(1-p);

即证明:-p^2/(1-p)*f[x]<=1/(1-p);

显然上式式成立的。

所以w(i,j)+w(i+1,j+1)<=w(i+1,j)+w(i,j+1)也成立。

所以dp[i]满足决策单调。我们可以用O(nlogn)的方法解决此题。

对于1D/1D动态规划问题,如何用决策单调的性质优化复杂度。可以看论文《1D/1D动态规划优化初步》。

代码如下:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
const int nn=110000;
const double eps=1e-8;
double f[nn];
double dp[nn];
int n,x;
double p;
double a[nn],b[nn];
void init()
{f[0]=0;for(int i=1;i<=n;i++){f[i]=(1.0+f[i-1])/(1-p);}
}
struct node
{int l,r;int val;node(){}node(int ll,int rr,int vall){l=ll,r=rr,val=vall;}
}que[nn];
int head,last;
bool check(int id,int v1,int v2)
{return (dp[v1]+f[id-v1]-(dp[v2]+f[id-v2]))<0;
}
void add(int l,int r,int val)
{while(head<last){if(check(que[last-1].l,que[last-1].val,val)){int xx=que[last-1].l,y=que[last-1].r;int mid;while(xx<y){mid=(xx+y+1)/2;if(check(mid,que[last-1].val,val)){xx=mid;}elsey=mid-1;}que[last-1].r=xx;l=xx+1;break;}else{l=que[last-1].l;last--;}}if(l<n+1){que[last++]=node(l,n,val);}
}
int main()
{int t,cas=1;int i;scanf("%d",&t);while(t--){scanf("%d%lf%d",&n,&p,&x);init();head=last=0;add(1,n,0);for(i=1;i<=n;i++){while(head<last){if(que[head].r>=i)break;head++;}dp[i]=dp[que[head].val]+f[i-que[head].val]+x;que[head].l=i+1;if(que[head].l>que[head].r)head++;add(n+1,n+1,i);}printf("Case #%d: %.6lf\n",cas++,dp[n]);}return 0;
}

方法二:枚举保存了几次,容易证明让几次保存平均分配是最优的。代码如下:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
const int nn=110000;
const double eps=1e-8;
double f[nn];
double dp[nn];
int n,x;
double p;
double a[nn],b[nn];
void init()
{f[0]=0;for(int i=1;i<=n;i++){f[i]=(1.0+f[i-1])/(1-p);}
}
int main()
{int t,cas=1;int i;scanf("%d",&t);while(t--){scanf("%d%lf%d",&n,&p,&x);init();double ans=1e20;for(i=0;i<=n-1;i++){int ix=n/(i+1);int fc=n%(i+1);double tem=fc*f[ix+1]+(i+1-fc)*f[ix]+(i+1)*x;ans=min(ans,tem);}printf("Case #%d: %.6lf\n",cas++,ans);}return 0;
}



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

相关文章

hdu5236 Article(贪心+概率dp)

hdu5236 题目 &#xff08;抄别人的http://www.cnblogs.com/qscqesze/p/4543740.html&#xff09;要求输入一篇N个字符的文章&#xff0c;对所有非负整数i&#xff1a; 每到第i0.1秒时可以输入一个文章字符 每到第i0.9秒时有P的概率崩溃&#xff08;回到开头或者上一个存盘…

JZOJ 5236. 【NOIP2017模拟8.7A组】利普希茨

5236. 【NOIP2017模拟8.7A组】利普希茨 (File IO): input:lipschitz.in output:lipschitz.out Time Limits: 1000 ms Memory Limits: 524288 KB Detailed Limits Description Input 输入文件名为lipschitz.in。 第一行一个整数n。 接下来一行n个整数&#xff0c;描述序列…

[jzoj5236]【NOIP2017模拟8.7A组】利普希茨

这道像数据结构的结论题传送门 我觉得这断不能怪我 一上来给出操作种类和 Log 形式的数据范围有如套路一般 Solution 60p 容易想到分治 对于整个序列&#xff0c;可以割作三份&#xff0c;分界点为最大值和最小值 因为 如果有一个 (i,j) 跨过了 分界点 k 那么 (i,k)|…

【贪心】hdu5236

题意 DRD经常使用一个文本处理软件&#xff0c;这个软件每输入一个字符就有一定的概率(p)崩溃&#xff0c;并且丢失上次保存之后的所有数据。执行一次保存需要x字符的代价&#xff08;但是不会崩溃&#xff09;问在最优策略下&#xff0c;输入字符的期望是多少 做法 一开始想到…

HDU5236 Article(期望dp)

Article 传送门1传送门2 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…

英韧IG5236开卡工具的量产使用教程,IG5236固态开卡简单过程

英韧IG5236固态开卡工具是一款用于量产的工具&#xff0c;可以帮助用户快速对固态硬盘进行开卡&#xff0c;提高生产效率。本教程将详细介绍如何使用IG5236固态开卡工具进行量产。 第一步&#xff1a;准备工作 首先&#xff0c;需要准备好以下材料&#xff1a; 1. IG5236固态…

HDU 5236 Article(概率DP)

http://acm.hdu.edu.cn/showproblem.php?pid5236 题意&#xff1a;现在有人要在文本编辑器中输入n个字符&#xff0c;然而这个编辑器有点问题。 在i0.1s&#xff08;i>0&#xff09;的时刻可以输入一个字符。 在i0.9s&#xff08;i>0&#xff09;的时刻系统可能会崩溃&a…

Article HDU - 5236(概率DP)

题目 https://cn.vjudge.net/problem/HDU-5236 题意 一个打字机 在打完一个字后会有p概率坏掉&#xff0c;只能保留之前保存的&#xff0c;保存需要x秒并且期间打字机不会坏掉 问一个人打n个字采取什么策略需要的时间期望最小是多少 思路 期望DP dp[i] (dp[i-1] 1) / (…