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][2][2];
int vis[maxn][maxn][2][2];
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 0;vis[l][r][f1][f2]=1;double ans = dp[l][r][f1][f2];ans+=p*0.5*(min(h*1.00,v[l]-v[l-1]-f1*h)+dfs(l+1,r,0,f2));//最左边那个朝左边倒ans+=(1-p)*0.5*(min(h*1.0,v[r+1]-v[r]-f2*h)+dfs(l,r-1,f1,0));//最右边那个朝右边倒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-1]-f1*h));else ans+=p*0.5*(v[r]-v[R]+h+dfs(l,R-1,f1,1));if(L>=r)ans+=(1-p)*0.5*(v[r]-v[l]+min(h,v[r+1]-v[r]-f2*h));else ans+=(1-p)*0.5*(v[L]-v[l]+h+dfs(L+1,r,1,f2));dp[l][r][f1][f2] = ans;return ans;
}
int main()
{scanf("%d%lf",&n,&h);scanf("%lf",&p);for(int i=1;i<=n;i++)scanf("%d",&v[i]);sort(v+1,v+1+n);v[n+1]=inf;v[0]=-inf;dr[n]=n;dl[1]=1;for(int i=n-1;i>=1;i--){if(v[i+1]-v[i]<h)dr[i]=dr[i+1];else dr[i]=i;}for(int i=2;i<=n;i++){if(v[i]-v[i-1]<h)dl[i]=dl[i-1];else dl[i]=i;}//cout<<dfs(1,n,0,0)<<endl;printf("%.15f\n",dfs(1,n,0,0));
}

Codeforces Round #331 (Div. 2) D. Wilbur and Trees 记忆化搜索相关推荐

  1. 思维dp ---- Codeforces Round #711 (Div. 2) - C. Planar Reflections[dp/记忆化搜索]

    题目链接 题目大意: 就是给你n个平面和一个寿命为k的衰变粒子.开始粒子从左向右飞行,粒子每经过一个平面就会产生一个副本粒子,这个副本粒子比原粒子的寿命少1,即为k-1,并且飞行方向是原粒子的反方向. ...

  2. Codeforces Round #459 (Div. 2) C 思维,贪心 D 记忆化dp

    Codeforces Round #459 (Div. 2) C. The Monster 题意:定义正确的括号串,是能够全部匹配的左右括号串. 给出一个字符串,有 (.). ? 三种字符, ? 可以 ...

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

  4. Codeforces Round #303 (Div. 2) E. Paths and Trees 最短路+贪心

    题目链接: 题目 E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes inputs ...

  5. Codeforces Round #548 (Div. 2), problem: (C) Edgy Trees 【并查集+快速幂】

    题意 给了一棵树,n个点,m条边.让从中选k个点,使得从a1到a2,a2到a3,ak-1到ak的路径中至少经过一条黑色的边,问这样的集合有多少个 思路 用并查集统计一个连通块的节点个数,最后用总的减去 ...

  6. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  7. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  8. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...

  9. Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...

最新文章

  1. Sharding-jdbc教程:Mysql数据库主从搭建
  2. path cp mv cat more less tail
  3. java和python的比较-java和python的比较
  4. 【C 语言】数组 ( 数组指针 | 数组指针定义 | 使用 数组类型* 定义数组指针 )
  5. RuoYi(若依开源框架)-前后台分离版-后端流程简单分析
  6. 6. Qt 信号与信号槽(11)Qt::ConnectionType类型
  7. ITK:通过镜像填充图像
  8. 文件及文件组备份与还原示例.sql
  9. TensorFlow升级1.4:Cannot remove entries from nonexistent file \lib\site-pack
  10. ieee期刊_论文绘图神器来了:一行代码绘制不同期刊格式图表,哈佛博士后开源...
  11. 层遇到select框时[收藏]
  12. DBA必知的mysql备份与还原的几大方法
  13. mybatis查询树形数据的两种方法
  14. 驱动人生官网服务器维护,驱动人生在线检测服务 驱动检测更新更加方便
  15. Redis入门指南(三)
  16. 服务器桌面假死怎么处理,win10桌面假死如何解决
  17. 黑苹果(Hackintosh)简单步骤教程
  18. 关于MySql中explain结果filtered的理解
  19. 从提示框:适用于Windows的iPad接口仿真,Easy Access iPhone手电筒和Kindle收藏管理...
  20. 计算机中大量文件需要管理怎么办,怎么处理目标文件系统文件过大

热门文章

  1. 蓝桥杯第八届省赛JAVA真题----拉马车
  2. 第一个express app 详细步骤
  3. winform数据传递到html,C#下winform和JS的互相调用和传参(webbrowser)
  4. java 数组下标6,这里为什么用equals会错,改==就不会?java入门第一集6.8获取数组下标课后练习...
  5. php 返回数组元素函数_php array_values 返回数组的所有值详解及实例
  6. idea 自动生产序列吗,IDEA自动生成序列化Id
  7. php多进程并发,php多进程模拟并发事务
  8. HDFS(名称节点与数据节点)简介
  9. Hadoop配置机架感知
  10. 最近比较火的一款字节产品