Topcoder SRM656div1 250 ( 期望DP )
Problem Statement
Charlie has N pancakes. He wants to serve some of them for breakfast. We will number the pancakes 0 through N-1. For each i, pancake i has width i+1 and deliciousness d[i].
Charlie chooses the pancakes he is going to serve using the following randomized process: He starts by choosing the first pancake uniformly at random from all the pancakes he has. He places the chosen pancake onto a plate. This pancake now forms the bottom of a future stack of pancakes. Then, Charlie repeats the following procedure:
If there are no more pancakes remaining, terminate.
Choose a pancake uniformly at random from the pancakes that have not been chosen yet.
If the width of this pancake is greater than the width of the pancake on top of the stack, terminate without taking it.
Place the chosen pancake on top of the stack and go back to step 1.
You are given the vector <int> d with N elements. The total deliciousness of a serving of pancakes is the sum of the deliciousness of all pancakes used in the serving. Compute and return the expected value of the total deliciousness of the pancakes chosen by Charlie.
Definition
Class:
RandomPancakeStack
Method:
expectedDeliciousness
Parameters:
vector <int>
Returns:
double
Method signature:
double expectedDeliciousness(vector <int> d)
(be sure your method is public)
Limits
Time limit (s):
2.000
Memory limit (MB):
256
Stack limit (MB):
256
Notes
-
Your return value must have an absolute or relative error smaller than or equal to 1e-6
Constraints
-
The number of elements in d will be between 1 and 250, inclusive.
-
Each element of d will be between 1 and 1,000, inclusive.
Examples
0)
{1,1,1}
Returns: 1.6666666666666667
The following scenarios may occur:
With probability 1/3, Charlie chooses pancake 0 first. In this case he won't be able to add any more pancakes and the total deliciousness of his serving of pancakes will be 1.
With probability 1/3, Charlie chooses pancake 1 first. What happens in the second round? With probability 1/2 he will choose pancake 0 and with probability 1/2 it will be pancake 2. In the first case the total deliciousness of Charlie's pancakes will be 2, in the second case it will be 1.
With probability 1/3, Charlie chooses pancake 2 first. If he chooses pancake 0 next, the total deliciousness of his pancakes will be 2. If he happens to choose pancake 1 next (followed by pancake 0 in the third round), the total deliciousness will be 3.
Summing this up, we get the expected deliciousness to be 1/3 * (1) + 1/3 * (1/2 * 1 + 1/2 * 2) + 1/3 * (1/2 * 2 + 1/2 * 3) = 5/3 = 1.666...
1)
{3,6,10,9,2}
Returns: 9.891666666666667
2)
{10,9,8,7,6,5,4,3,2,1}
Returns: 10.999999724426809
3)
{1,2,3,4,5,6,7,8,9,10}
Returns: 7.901100088183421
4)
{2,7,1,8,2,8,1,8,2,8,4,5,90,4,5,2,3,5,60,2,8,74,7,1}
Returns: 19.368705050402465
5)
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
Returns: 1.718281828459045
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
期望Dp, 不得不承认自己是个概率白痴,这么简单的问题都想了这么久
dp[i][j] 表示选到第i个 ,选了j 个还能选多少个的期望~
#include <bits/stdc++.h>
using namespace std;
const int N = ;
class RandomPancakeStack
{
public:
double dp[N][N] , dd[N];
bool vis[N][N];
int n ; double Dp( int i , int cnt ) {
if( i == ) return dd[] ;
if( !vis[i][cnt] ) {
vis[i][cnt] = true ;
double &c = dp[i][cnt] ;
c = dd[] ;
for( int j = ; j <= i ; ++j ) {
c += 1.0 * ( n - j - cnt ) / ( n - cnt - 1.0 ) * dd[j] + ( 1.0 - 1.0 * ( n - j - cnt ) / ( n - cnt - 1.0 ) ) * ( dd[j] + Dp( j - , cnt + ) ) ;
}
c *= 1.0 / i ;
}
return dp[i][cnt] ;
} double expectedDeliciousness( vector <int> d ){
n = (int) d.size() ;
for( int i = ; i < n ; ++i ) dd[i+] = ( double )d[i] ;
memset( vis , false , sizeof vis );
return Dp( n , );
}
};
Topcoder SRM656div1 250 ( 期望DP )的更多相关文章
- P3239 [HNOI2015]亚瑟王 期望 dp
LINK:亚瑟王 Saber!Excalibur! 比较难的期望dp. 可以发现如果暴力枚举所有的局面复杂度很高 . 转换的思路则是 期望的线性性. 求出每张牌的期望累加即可. 考虑每张牌的期望=这张 ...
- 【BZOJ-1419】Red is good 概率期望DP
1419: Red is good Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 660 Solved: 257[Submit][Status][Di ...
- [NOIP2016]换教室 D1 T3 Floyed+期望DP
[NOIP2016]换教室 D1 T3 Description 对于刚上大学的牛牛来说, 他面临的第一个问题是如何根据实际情况中情合适的课程. 在可以选择的课程中,有2n节课程安排在n个时间段上.在第 ...
- HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)
题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...
- 【BZOJ-4008】亚瑟王 概率与期望 + DP
4008: [HNOI2015]亚瑟王 Time Limit: 20 Sec Memory Limit: 512 MBSec Special JudgeSubmit: 832 Solved: 5 ...
- 期望dp BZOJ3450+BZOJ4318
BZOJ3450 概率期望DP f[i]表示到i的期望得分,g[i]表示到i的期望长度. 分三种情况转移: ① s[i]=‘x’:f[i]=f[i-1],g[i]=0 ② s[i]=‘o’:f[i]= ...
- HDU 4405 期望DP
期望DP算是第一题吧...虽然巨水但把思路理理清楚总是好的.. 题意:在一个1×n的格子上掷色子,从0点出发,掷了多少前进几步,同时有些格点直接相连,即若a,b相连,当落到a点时直接飞向b点.求走到n ...
- POJ 2096 【期望DP】
题意: 有n种选择,每种选择对应m种状态.每种选择发生的概率相等,每种选择中对应的每种状态发生的概率相等. 求n种选择和m种状态中每种至少发生一次的期望. 期望DP好别扭啊.要用倒推的方法. dp[i ...
- ZOJ 3822 Domination 期望dp
Domination Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem ...
随机推荐
- vscode-函数注释插件-正则插件
1.安装插件KoroFileHeader(函数标准注释) 2.设置 在vscode左下角点击设置按钮,选择“设置”,然后输入“fileheader”, 文件头部注释:Fileheader:custom ...
- DUBBO原理、应用与面经总结
研读dubbo源码已经有一段时间了,dubbo中有非常多优秀的设计模式和示例代码值得学习,但是dubbo的调用层级和方法链都较为繁杂,如果不对源码思路进行梳理则很容易忘却,因此总结一篇研读心得,从阅读 ...
- 【LuoguP4916】魔力环
题目链接 题意 求出 \(n\) 个珠子的在旋转同构意义下的手 环 个数,满足以下条件: 恰好有 \(m\) 个黑色珠子,其余为白色. 黑色珠子形成的最长连续段不能超过 \(k\) 个. Sol 考虑 ...
- 《转》tensorflow学习笔记
from http://m.blog.csdn.net/shengshengwang/article/details/75235860 1. RNN结构 解析: (1)one to one表示单输入单 ...
- Linux学习-通过loganalyzer展示MySQL中rsyslog日志
一.实验环境 系统:CentOS7.6 软件包:apache,php,mariadb-server (都是基于光盘yum源) 源码包:loganalyzer-4.1.7.tar.gz (http:// ...
- HTML中的表单<form>标签
一.HTML表单 HTML 表单用于搜集不同类型的用户输入. HTML 表单包含表单元素,表单元素指的是不同类型的 input 元素.复选框.单选按钮.提交按钮等等. 关于表单的更多内容可以参考htt ...
- 2,ActiveMQ-入门
ActiveMQ是Apache出品的,非常流行的消息中间件,可以说要掌握消息中间件,需要从ActiveMQ开始.首先去官网下载:ActiveMQ官网 一,ActiveMQ目录配置文件 1.1,Acti ...
- 状态管理工具对比vuex、redux、flux
1.为什么要使用状态管路工具 在跨层级的组件之间传递信息,尤其是复杂的组件会非常困难.也不利于开发和维护,这时我们就a需要用到状态管理工具. 2.Flux
- docker for windows 中 镜像 microsoft/donet 的文件结构
一共有3个文件夹和1个文件: [Program Files] [Users] [Windows] License.txt 使用vs默认生成的dockerfile文件生成后,会新增app文件夹.
- PHP高手干货分享:不能不看的50个细节!【PHP培训教程】
兄弟连分享PHP高手干货:不能不看的50个细节 1.用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以 ...