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

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

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

n<=2000.

标程:

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<cstring>
  4. using namespace std;
  5. const int inf=1e8+;
  6. const int N=;
  7. int pos[N],h,n,L[N],R[N];
  8. double p,dp[N][N][][];
  9. int check(int op,int x,int dir)
  10. {
  11. if (op==)
  12. {
  13. if (!dir) return min(pos[x]-pos[x-],h);
  14. else return min(h,max(pos[x]-pos[x-]-h,));
  15. }else {
  16. if (!dir) return min(h,max(pos[x+]-pos[x]-h,));
  17. else return min(pos[x+]-pos[x],h);
  18. }
  19. }
  20. double dfs(int x,int y,int l,int r)
  21. {
  22. if (x>y) return ;
  23. if (dp[x][y][l][r]) return dp[x][y][l][r];
  24. double &ans=dp[x][y][l][r];
  25. //choose left
  26. ans+=0.5*p*(dfs(x+,y,,r)+check(,x,l));//fall left
  27. if (R[x]+<=y) ans+=0.5*(-p)*(dfs(R[x]+,y,,r)+pos[R[x]]-pos[x]+h);//fall right
  28. else ans+=0.5*(-p)*(pos[y]-pos[x]+check(,y,r));
  29. //choose right
  30. ans+=0.5*(-p)*(dfs(x,y-,l,)+check(,y,r));//fall right
  31. if (x<=L[y]-) ans+=0.5*p*(dfs(x,L[y]-,l,)+pos[y]-pos[L[y]]+h);//fall left
  32. else ans+=0.5*p*(pos[y]-pos[x]+check(,x,l));
  33. return ans;
  34. }
  35. int main()
  36. {
  37. scanf("%d%d%lf",&n,&h,&p);
  38. for (int i=;i<=n;i++) scanf("%d",&pos[i]);
  39. sort(pos+,pos+n+);pos[]=pos[]-h;pos[n+]=pos[n]+h;
  40. L[]=;R[n]=n;
  41. for (int i=;i<=n;i++)
  42. if (i==||pos[i]-pos[i-]<h) L[i]=L[i-];else L[i]=i;
  43. for (int i=n-;i>=;i--)
  44. if (i==n||pos[i+]-pos[i]<h) R[i]=R[i+];else R[i]=i;
  45. printf("%.10lf\n",dfs(,n,,));
  46. return ;
  47. }

易错点: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. delphi基础篇之项目文件

    delphi基础篇之项目文件   program Teacher2018; uses   Forms, Unit1 in 'Unit1.pas' {Form1}, Unit2 in 'Unit2.pa ...

  2. ps-通道错位制作奇幻海报

    效果图 素材 1.载入素材 点击通道 点击下面的蓝色通道,全选-复制 点击绿色通道,全选-黏贴 编辑-变化-水平翻转 点击RGB即可.

  3. python学习3—数据类型之整型、字符串和布尔值

    python学习3-数据类型之整型.字符串和布尔值 数据类型 python3支持的数据类型共有6种: 1 Number 2 String 3 List 4 Tuple 5 Set 6 Dictiona ...

  4. Southeastern European Regional Programming Contest 2019

    easy: I medium-easy: BDEGJ medium: F medium-hard: A A. B. 按 x 排序,\(dp[i][j][k]\) 表示考虑前 \(i\) 个物品,lev ...

  5. Mysql集群和主从

    1.Mysql cluster: share-nothing,分布式节点架构的存储方案,以便于提供容错性和高性能. 需要用到mysql cluster安装包,在集群中的每一个机器上安装. 有三个关键概 ...

  6. 【idea】设置console控制台显示内容大小

    Settings→Editor→General→Console 参考文章:https://blog.csdn.net/weixin_34363171/article/details/93444586

  7. 360自动抢票还不够,几行js代码设置无人值守

    360就是牛逼哄哄的...... 但是最近在使用360浏览器抢票的时候还是发现了一些体验不好的地方,比如搞着搞着就退出了登录,有时候能帮你自动登录进去,但是自动登录之后又不会帮你自动开始抢.然后验证码 ...

  8. sql count 函数用法

    count(*) 会查询所有记录数,,包括为null值的数据: count(column)不会包含 column值为null的情况: count(1) 和 count(*)相同,,不同的是,,mysq ...

  9. react添加多个域名proxy代理,跨域

    在package.json中加入如下: { "name": "demo", "version": "0.1.0", &q ...

  10. JAVA数据结构之哈希表

    Hash表简介: Hash表是基于数组的,优点是提供快速的插入和查找的操作,编程实现相对容易,缺点是一旦创建就不好扩展,当hash表被基本填满的时候,性能下降非常严重(发生聚集引起的性能的下降),而且 ...