Codeforces Round #331 (Div. 2) D. Wilbur and Trees 记忆化搜索
D. Wilbur and Trees
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/596/problem/D
Description
Wilbur the pig really wants to be a beaver, so he decided today to pretend he is a beaver and bite at trees to cut them down.
There are n trees located at various positions on a line. Tree i is located at position xi. All the given positions of the trees are distinct.
The trees are equal, i.e. each tree has height h. Due to the wind, when a tree is cut down, it either falls left with probability p, or falls right with probability 1 - p. If a tree hits another tree while falling, that tree will fall in the same direction as the tree that hit it. A tree can hit another tree only if the distance between them is strictly less than h.
For example, imagine there are 4 trees located at positions 1, 3, 5 and 8, while h = 3 and the tree at position 1 falls right. It hits the tree at position 3 and it starts to fall too. In it's turn it hits the tree at position 5 and it also starts to fall. The distance between 8 and 5 is exactly 3, so the tree at position 8 will not fall.
As long as there are still trees standing, Wilbur will select either the leftmost standing tree with probability 0.5 or the rightmost standing tree with probability 0.5. Selected tree is then cut down. If there is only one tree remaining, Wilbur always selects it. As the ground is covered with grass, Wilbur wants to know the expected total length of the ground covered with fallen trees after he cuts them all down because he is concerned about his grass-eating cow friends. Please help Wilbur.
Input
The first line of the input contains two integers, n (1 ≤ n ≤ 2000) and h (1 ≤ h ≤ 108) and a real number p (0 ≤ p ≤ 1), given with no more than six decimal places.
The second line of the input contains n integers, x1, x2, ..., xn ( - 108 ≤ xi ≤ 108) in no particular order.
Output
Print a single real number — the expected total length of the ground covered by trees when they have all fallen down. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
Sample Input
2 2 0.500000
1 2
Sample Output
3.250000000
HINT
题意
在一个平面上有n棵树,每棵树高为h,你是一个伐木工人,每次有1/2的概率选择砍掉最左边或者最右边的树
树也有p的概率向左倒,(1-p)的概率向右倒
树如果倒下的时候,压中了别的树,那么那棵树也会跟着倒下
然后问你,最后倒下的树的期望长度总和是多少
题解:
区间dp,dfs(l,r,f1,f2)
f1表示这个l-1这棵树是否倒向了右边,f2表示r+1这棵树是否倒向了左边
说是dp,实质上就是dfs,直接枚举所有的情况暴力dfs就好了
代码
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define maxn 2005
const int inf = 1e9;
double dp[maxn][maxn][][];
int vis[maxn][maxn][][];
int n;
double h,p;
int v[maxn];
int dl[maxn],dr[maxn];
double dfs(int l,int r,int f1,int f2)
{
//cout<<l<<" "<<r<<" "<<f1<<" "<<f2<<endl;
if(vis[l][r][f1][f2])return dp[l][r][f1][f2];
if(l>r)return ;
vis[l][r][f1][f2]=;
double ans = dp[l][r][f1][f2];
ans+=p*0.5*(min(h*1.00,v[l]-v[l-]-f1*h)+dfs(l+,r,,f2));//最左边那个朝左边倒
ans+=(-p)*0.5*(min(h*1.0,v[r+]-v[r]-f2*h)+dfs(l,r-,f1,));//最右边那个朝右边倒
int L = dr[l];//左边向右边倒
int R = dl[r];//右边向左边倒
if(R<=l)ans+=p*0.5*(v[r]-v[l]+min(h,v[l]-v[l-]-f1*h));
else ans+=p*0.5*(v[r]-v[R]+h+dfs(l,R-,f1,));
if(L>=r)ans+=(-p)*0.5*(v[r]-v[l]+min(h,v[r+]-v[r]-f2*h));
else ans+=(-p)*0.5*(v[L]-v[l]+h+dfs(L+,r,,f2));
dp[l][r][f1][f2] = ans;
return ans;
}
int main()
{
scanf("%d%lf",&n,&h);
scanf("%lf",&p);
for(int i=;i<=n;i++)
scanf("%d",&v[i]);
sort(v+,v++n);
v[n+]=inf;
v[]=-inf;
dr[n]=n;dl[]=;
for(int i=n-;i>=;i--)
{
if(v[i+]-v[i]<h)dr[i]=dr[i+];
else dr[i]=i;
}
for(int i=;i<=n;i++)
{
if(v[i]-v[i-]<h)dl[i]=dl[i-];
else dl[i]=i;
}
//cout<<dfs(1,n,0,0)<<endl;
printf("%.15f\n",dfs(,n,,));
}
Codeforces Round #331 (Div. 2) D. Wilbur and Trees 记忆化搜索的更多相关文章
- Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)
D. MADMAX time limit per test1 second memory limit per test256 megabytes Problem Description As we a ...
- Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
- Codeforces Round #331 (Div. 2) E. Wilbur and Strings dfs乱搞
E. Wilbur and Strings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596 ...
- Codeforces Round #331 (Div. 2)C. Wilbur and Points 贪心
C. Wilbur and Points Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596/ ...
- Codeforces Round #331 (Div. 2) B. Wilbur and Array 水题
B. Wilbur and Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596/p ...
- Codeforces Round #331 (Div. 2) A. Wilbur and Swimming Pool 水题
A. Wilbur and Swimming Pool Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...
- Codeforces Round #331 (Div. 2) C. Wilbur and Points
C. Wilbur and Points time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #331 (Div. 2) _A. Wilbur and Swimming Pool
A. Wilbur and Swimming Pool time limit per test 1 second memory limit per test 256 megabytes input s ...
- Codeforces Round #331 (Div. 2) B. Wilbur and Array
B. Wilbur and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- Android裁剪固定大小头像的功能
转载自: http://www.eoeandroid.com/thread-497277-1-1.html 效果很好,特意转载过来记录一下,加深一下印象. 效果就是 :中间的方框不动,可以拖动图片,选 ...
- ioctl用法详解 (网络)
本函数影响由fd参数引用的一个打开的文件. #include#include int ioctl( int fd, int request, .../* void *arg */ );返回0:成功 ...
- VelocityTracker简单用法
VelocityTracker顾名思义即速度跟踪,在android中主要应用于touch event, VelocityTracker通过跟踪一连串事件实时计算出 当前的速度,这样的用法在androi ...
- acdream 1056 (黑白染色)
题意:给你一些关系,每个关系是两只马的名字,表示这两个马不能在一个分组里,问你能否将这些马分成两组. 黑白染色,相邻的点染不同颜色.bfs搞即可,水题. /* * this code is made ...
- MorningSale 介绍
MorningSale是一个WEB端的收集门店销售数据,显示销售数据的简单系统,我相信该系统能够有效的提高销售公司在门店销售数据收集 汇总 分析方面的工作效率. 主要功能介绍如下: 1.查看某个店面 ...
- 通过gdb调试分析Linux内核的启动过程
作者:吴乐 山东师范大学 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.实验流程 1.打开环境 执 ...
- bzoj 2438 [中山市选2011]杀人游戏(SCC+概率)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2438 [题意] N个人中有一个杀手,每次询问一个人可能被杀或被告知其认识的人中谁是杀手 ...
- mvc bundle功能(1)
现如今都提倡敏捷开发,快速开发,但是再要求速度的同时,还得保证质量!前端我是没办法,毕竟是直接要面向用户的,但是后台,解决方案那就多了,诸如extjs,bootstrap,kendoui,都可以解决. ...
- Javascript手记-执行环境和作用域
执行环境是javascript一个重要的概念,执行环境定义了变量有权访问其他数据决定了他们各自的行为,每个执行环境 都有一个与之关联的变量,环境中定义的所有变量和函数都保存在这个对象中,虽然我们编写的 ...
- 分布式文件系统-HDFS
HDFS Hadoop的核心就是HDFS与MapReduce.那么HDFS又是基于GFS的设计理念搞出来的. HDFS全称是Hadoop Distributed System.HDFS是为以流的方式存 ...