Codeforces 596D Wilbur and Trees dp (看题解)
一直在考虑, 每一段的贡献, 没想到这个东西能直接dp。。因为所有的h都是一样的。
#include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ull unsigned long longusing namespace std;const int N = 2000 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1);int n, h, x[N], lft[N], rgt[N]; double ans, p, pr, pl; double f[N][N][2][2]; bool vis[N][N][2][2];double dp(int i, int j, int u, int v) {if(i > j) return 0;if(vis[i][j][u][v]) return f[i][j][u][v];double& ans = f[i][j][u][v];double tmp1 = 0, tmp2 = 0;if(!u) ans += 0.5 * pl * (dp(i + 1, j, 0, v) + min(h, x[i] - x[i - 1])), tmp1 = min(h, x[i] - x[i - 1]);else ans += 0.5 * pl * (dp(i + 1, j, 0, v) + min(h, x[i] - x[i - 1] - h)), tmp1 = min(h, x[i] - x[i - 1] - h);if(v) ans += 0.5 * pr * (dp(i, j - 1, u, 1) + min(h, x[j + 1] - x[j])), tmp2 = min(h, x[j + 1] - x[j]);else ans += 0.5 * pr * (dp(i, j - 1, u, 1) + min(h, x[j + 1] - h - x[j])), tmp2 = min(h, x[j + 1] - h - x[j]);int L = min(rgt[i], j), R = max(lft[j], i);ans += 0.5 * pr * (dp(L + 1, j, 1, v) + x[L] - x[i] + (L != j ? h : tmp2));ans += 0.5 * pl * (dp(i, R - 1, u, 0) + x[j] - x[R] + (R != i ? h : tmp1));vis[i][j][u][v] = true;return ans; }int main() {scanf("%d%d%lf", &n, &h, &p);pl = p, pr = 1 - p;for(int i = 1; i <= n; i++) scanf("%d", &x[i]);x[0] = -inf, x[n + 1] = inf;sort(x + 1, x + 1 + n);lft[1] = 1, rgt[n] = n;for(int i = 2; i <= n; i++)lft[i] = x[i] - h < x[i - 1] ? lft[i - 1] : i;for(int i = n - 1; i >= 1; i--)rgt[i] = x[i] + h > x[i + 1] ? rgt[i + 1] : i;printf("%.10f\n", dp(1, n, 0, 1));return 0; }/* */
转载于:https://www.cnblogs.com/CJLHY/p/10512296.html
Codeforces 596D Wilbur and Trees dp (看题解)相关推荐
- Codeforces 596D Wilbur and Trees
http://codeforces.com/contest/596/problem/D 题目大意: 有n棵树排成一排,高度都为h. 主人公要去砍树,每次等概率地随机选择没倒的树中最左边的树或者最右边的 ...
- codeforces 711 C. Coloring Trees (dp)
题目:http://codeforces.com/group/1EzrFFyOc0/contest/711/problem/C 题意:有n棵树,m种颜料,部分树有初始颜色,如为0,则没有初始颜色,给树 ...
- Codeforces 213E Two Permutations 线段树 (看题解)
Two Permutations 关键是没想到按大小顺序把第二个排列一个一个加入线段树, 然后线段树维护整体的hash值, 得到的hs值减去一个sub 之后与, 第一个排列的hash值比较. #inc ...
- 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 ...
- CodeForces - 1509C The Sports Festival(dp)
题目链接:点击查看 题目大意:给出一个长度为 nnn 的数列,现在需要对其进行重排列,使得贡献之和最小 对于一个排列的贡献来说,对于每个 iii ,则 di=max(a1,a2,...,ai)−min ...
- CodeForces - 1312E Array Shrinking(区间dp)(通俗易懂)
CodeForces - 1312E Array Shrinking(区间dp) 题目链接: 没做出来,看了一下别人的题解,才A掉.但网上没发现一篇讲得比较易懂的题解,所以就准备写一篇再加上我自己的理 ...
- ZOJ 3962 Seven Segment Display(数位DP)题解
题意:给一个16进制8位数,给定每个数字的贡献,问你贡献和. 思路:数位DP,想了很久用什么表示状态,看题解说用和就行,其他的都算是比较正常的数位DP. 代码: #include<iostrea ...
- Educational Codeforces Round 36 (Rated for Div. 2) 题解
Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到 ...
- 【CodeForces 1253C --- Sweets Eating】DP
[CodeForces 1253C --- Sweets Eating]DP Description Tsumugi brought n delicious sweets to the Light M ...
最新文章
- leetcode 202 快乐数
- OneZero第四周第四次站立会议(2016.4.14)
- ajax html例子,AJAX实例
- Merge Sort及其对一类问题的应用
- 响应式关系数据库处理R2DBC
- 提取过程_大米多肽提取过程中如何应用膜分离技术呢?
- JSON简介,语法,在html中应用
- SAP License:MIRO
- 如何用python画长方形_Python 画矩形
- Sklearn——保存模型参数(picklejoblib)
- 如何选择学习主流程序语言(一)
- php 基础 自动类型转换
- python模块之subprocess
- arm上移植iconv命令
- 工科数学分析寒假预习day2 正项级数的敛散性
- html倒计时星期日,功能齐全的jQuery倒计时插件
- python gif压缩_压缩gif的正确姿势
- matlab实现转换音频格式文件,mp3到wav的转换
- 基于html的明星网站
- java程序员出路在哪里,使用/教程/实例