题意:有一些高度为h的树在数轴上。每次选择剩下的树中最左边或是最右边的树推倒(各50%概率),往左倒有p的概率,往右倒1-p。

一棵树倒了,如果挨到的另一棵树与该数的距离严格小于h,那么它也会往同方向倒。

问所有树都被推倒后的期望覆盖长度?

n<=2000.

标程:

 #include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int inf=1e8+;
const int N=;
int pos[N],h,n,L[N],R[N];
double p,dp[N][N][][];
int check(int op,int x,int dir)
{
if (op==)
{
if (!dir) return min(pos[x]-pos[x-],h);
else return min(h,max(pos[x]-pos[x-]-h,));
}else {
if (!dir) return min(h,max(pos[x+]-pos[x]-h,));
else return min(pos[x+]-pos[x],h);
}
}
double dfs(int x,int y,int l,int r)
{
if (x>y) return ;
if (dp[x][y][l][r]) return dp[x][y][l][r];
double &ans=dp[x][y][l][r];
//choose left
ans+=0.5*p*(dfs(x+,y,,r)+check(,x,l));//fall left
if (R[x]+<=y) ans+=0.5*(-p)*(dfs(R[x]+,y,,r)+pos[R[x]]-pos[x]+h);//fall right
else ans+=0.5*(-p)*(pos[y]-pos[x]+check(,y,r));
//choose right
ans+=0.5*(-p)*(dfs(x,y-,l,)+check(,y,r));//fall right
if (x<=L[y]-) ans+=0.5*p*(dfs(x,L[y]-,l,)+pos[y]-pos[L[y]]+h);//fall left
else ans+=0.5*p*(pos[y]-pos[x]+check(,x,l));
return ans;
}
int main()
{
scanf("%d%d%lf",&n,&h,&p);
for (int i=;i<=n;i++) scanf("%d",&pos[i]);
sort(pos+,pos+n+);pos[]=pos[]-h;pos[n+]=pos[n]+h;
L[]=;R[n]=n;
for (int i=;i<=n;i++)
if (i==||pos[i]-pos[i-]<h) L[i]=L[i-];else L[i]=i;
for (int i=n-;i>=;i--)
if (i==n||pos[i+]-pos[i]<h) R[i]=R[i+];else R[i]=i;
printf("%.10lf\n",dfs(,n,,));
return ;
}

易错点:1.注意长度的判断。分类讨论。

2.需要设置极值端点,和1树、n树的距离要>=h。

题解:区间dp

剩下的一段树一定是连续的,区间dp即可。分四种情况讨论推导情况。

预处理一棵树往左/往右倒影响多少棵树。注意计算距离。

CF596D Wilbur and Trees的更多相关文章

  1. 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/p ...

  2. Codeforces 596D Wilbur and Trees

    http://codeforces.com/contest/596/problem/D 题目大意: 有n棵树排成一排,高度都为h. 主人公要去砍树,每次等概率地随机选择没倒的树中最左边的树或者最右边的 ...

  3. Codeforces 596D Wilbur and Trees dp (看题解)

    一直在考虑, 每一段的贡献, 没想到这个东西能直接dp..因为所有的h都是一样的. #include<bits/stdc++.h> #define LL long long #define ...

  4. codeforces 几道题目

    BZOJ挂了....明天就要出发去GDKOI了....不能弃疗. 于是在cf水了几道题, 写写详(jian)细(dan)题解, 攒攒RP, 希望GDKOI能好好发挥.......  620E. New ...

  5. [C#] C# 知识回顾 - 表达式树 Expression Trees

    C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...

  6. hdu2848 Visible Trees (容斥原理)

    题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...

  7. [LeetCode] Minimum Height Trees 最小高度树

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  8. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  9. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

随机推荐

  1. 9.RabbitMQ Topic类型交换机

    RabbitMQ消息服务中Topic类型交换机根据通配符路由消息,*代表一个单词,#代表代表0或多个单词.   生产者 消费者   代码 Producer.java   package com.tes ...

  2. js用document.getElementById时要注意!

    <!DOCTYPE html> <html lang="en"> <head> <script src="http://code ...

  3. 用python, PIL在图像上添加文字(可以控制,调节为水印等)

    最近想在图像上,添加想要的文字,首先想到的是matplotlib,但是这个更加倾向于画图(柱状图,折线图之类) opencv这个库肯定也行,但是为了和我现有程序连接在一起,我选择了PIL 其中字体的设 ...

  4. [JSOI2019]精准预测

    题目 这么明显的限制条件显然是\(\text{2-sat}\) 考虑按照时间拆点,\((0/1,x,t)\)表示\(x\)个人在时间\(t\)是生/死 有一些显然的连边 \[(0,x,t+1)-> ...

  5. rtsp+rtmp多路流媒体播放

    一.前言 之前博主有写过 一篇博文,讲的是使用videojs在谷歌浏览器网页上播放rtmp流媒体,具体可参考我之前的博客:https://www.cnblogs.com/FHC1994/p/99814 ...

  6. Permission denied: user=root, access=WRITE, inode="/":hdfs:supergroup:drwxr-xr-x

    通过手动安装CDH没权限 [root@slave1 ~]# groupadd supergroup[root@slave1 ~]# hadoop fs -mkdir /tao3^C[root@slav ...

  7. Java 学习 时间格式化(SimpleDateFormat)与历法类(Calendar)用法详解

    基于Android一些时间创建的基本概念 获取当前时间 方式一: Date date = new Date(); Log.e(TAG, "当前时间="+date); 结果: E/T ...

  8. CSIC_716_20191126【面向对象编程--继承】

    继承 什么是继承:继承是新建类的一种方式,通过此方式生成的类称为子类.或者 派生类,被继承的类称为父类.基类或超类.在python中,一个子类可以继承多个父类. 继承的作用:减少代码的冗余,提高开发效 ...

  9. JS函数进阶

    函数的定义方式 函数声明 函数表达式 new Function 函数声明  function foo () { } 函数表达式  var foo = function () { ​ } 函数声明与函数 ...

  10. 修改docker+jenkins挂载目录

    1.停止docker [root@jenkins data]# systemctl stop docker 2.创建目录,拷贝数据 [root@jenkins data]# mkdir -p /new ...