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 ...
随机推荐
- Spring Security初识
Spring Security与Spring Boot集成 添加依赖: <dependency> <groupId>org.springframework.boot</g ...
- HDU-6278-Jsut$h$-index(主席树)
链接: https://vjudge.net/problem/HDU-6278 题意: The h-index of an author is the largest h where he has a ...
- elementui多个文件上传问题
我认为绑定一个值 然后把值改变不同的名字即可
- iView栅格的使用
一般情况下栅格系统都会把每行row分为12列,但是iview是采用了24栅格系统,将区域进行24等分 基础用法 实例代码: <template> <Row> <Col s ...
- A1065
判断两数相加是否大于第三数,大于输出true,否则输出false(相等也是false) 1 需要注意数字溢出的问题: 2 先判断溢出,因为在a,b都是负数最小值的情况下,相加直接是正数,在c较小的时候 ...
- Cloneable接口的作用与深度克隆与浅度克隆
cloneable接口的作用 cloneable其实就是一个标记接口,只有实现这个接口后,然后在类中重写Object中的clone方法,然后通过类调用clone方法才能克隆成功,如果不实现这个接口,则 ...
- M(model)V(view)C(controller,serlvet),(分) 静态工厂模式,单例模式
- Activiti7整合SpringBoot(十二)
1 SpringBoot 整合 Activiti7 的配置 为了能够实现 SpringBoot 与 Activiti7 整合开发,首先我们要引入相关的依赖支持.所以,我们在工程的 pom.xml 文件 ...
- android平台上AES,DES加解密及问题
在使用java进行AES加密的时候,会用到如下方法: SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); 但是在andr ...
- HPU personal training
K - Two Contests 原题链接:https://agc040.contest.atcoder.jp/tasks/agc040_b?lang=en 题目大意: 给一个区间集合,将这些区间分为 ...