codeforces 261B Maxim and Restaurant(概率DP)
2 seconds
256 megabytes
standard input
standard output
Maxim has opened his own restaurant! The restaurant has got a huge table, the table's length is p meters.
Maxim has got a dinner party tonight, n guests will come to him. Let's index the guests of Maxim's restaurant from 1 to n. Maxim knows the sizes of all guests that are going to come to him. The i-th guest's size (ai) represents the number of meters the guest is going to take up if he sits at the restaurant table.
Long before the dinner, the guests line up in a queue in front of the restaurant in some order. Then Maxim lets the guests in, one by one. Maxim stops letting the guests in when there is no place at the restaurant table for another guest in the queue. There is no place at the restaurant table for another guest in the queue, if the sum of sizes of all guests in the restaurant plus the size of this guest from the queue is larger than p. In this case, not to offend the guest who has no place at the table, Maxim doesn't let any other guest in the restaurant, even if one of the following guests in the queue would have fit in at the table.
Maxim is now wondering, what is the average number of visitors who have come to the restaurant for all possible n! orders of guests in the queue. Help Maxim, calculate this number.
The first line contains integer n (1 ≤ n ≤ 50) — the number of guests in the restaurant. The next line contains integers a1, a2, ..., an(1 ≤ ai ≤ 50) — the guests' sizes in meters. The third line contains integer p (1 ≤ p ≤ 50) — the table's length in meters.
The numbers in the lines are separated by single spaces.
In a single line print a real number — the answer to the problem. The answer will be considered correct, if the absolute or relative error doesn't exceed 10 - 4.
3
1 2 3
3
1.3333333333
In the first sample the people will come in the following orders:
- (1, 2, 3) — there will be two people in the restaurant;
- (1, 3, 2) — there will be one person in the restaurant;
- (2, 1, 3) — there will be two people in the restaurant;
- (2, 3, 1) — there will be one person in the restaurant;
- (3, 1, 2) — there will be one person in the restaurant;
- (3, 2, 1) — there will be one person in the restaurant.
In total we get (2 + 1 + 2 + 1 + 1 + 1) / 6 = 8 / 6 = 1.(3).
题解:
令dp[i][j][k]表示前i个取j个和为k的期望
考虑转移到i的时候,无非是a[i]取或者不取
取的次数占总次数的比为c(i-1,j-1)/c(i,j)=j/i
所以不取的占比为1-j/i=(i-j)/i
故得到转移方程
dp[i][j][k]=j/i*dp[i-1][j-1][k-a[i]]+(i-j)/i*dp[i-1][j][k]
代码如下:
#include<bits/stdc++.h>
using namespace std; double dp[][][];
double fac[];
int n,p,a[]; int main()
{
dp[][][]=1.0;
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
scanf("%d",&p);
for(int i=;i<=n;i++)
{
for(int j=;j<=i;j++)
{
for(int k=;k<=p;k++)
{
dp[i][j][k]+=dp[i-][j][k]*(i-j)/i;
if(k-a[i]>=)
{
dp[i][j][k]+=dp[i-][j-][k-a[i]]*j/i;
}
}
}
}
double ans=0.0;
for(int i=;i<=n;i++)
{
for(int j=;j<=p;j++)
{
ans+=dp[n][i][j];
}
}
printf("%.5lf\n",ans);
}
codeforces 261B Maxim and Restaurant(概率DP)的更多相关文章
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces 261B Maxim and Restaurant 解法汇总
题意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a3-+ai,问满足Si<=p的i的最大值的期望.(p<=50) 这道题在网上有一些不同 ...
- CodeForces 540D--Bad Luck Island(概率DP)
貌似竟然是我的第一道概率DP.. 手机码代码真不舒服.... /************************************************ Memory: 67248 KB Ti ...
- codeforces 148D Bag of mice(概率dp)
题意:给你w个白色小鼠和b个黑色小鼠,把他们放到袋子里,princess先取,dragon后取,princess取的时候从剩下的当当中任意取一个,dragon取得时候也是从剩下的时候任取一个,但是取完 ...
- CodeForces 24D Broken robot (概率DP)
D. Broken robot time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- 【CodeForces 261B】Maxim and Restaurant(DP,期望)
题目链接 第一种解法是$O(n^3*p)$的:f[i][j][k]表示前i个人进j个人长度为k有几种方案(排列固定为123..n时).$f[i][j][k]=f[i-1][j][k]+f[i-1][j ...
- Codeforces 148D Bag of mice 概率dp(水
题目链接:http://codeforces.com/problemset/problem/148/D 题意: 原来袋子里有w仅仅白鼠和b仅仅黑鼠 龙和王妃轮流从袋子里抓老鼠. 谁先抓到白色老师谁就赢 ...
- CodeForces 148D-Bag of mice(概率dp)
题意: 袋子里有w个白球b个黑球,现在两个人轮流每次取一个球(不放回),先取到白球的获胜,当后手取走一个球时,袋子里的球会随机的漏掉一个,问先手获胜的概率. 分析: dp[i][j]表示袋子中i个白球 ...
- Codeforces 1151F Sonya and Informatics (概率dp)
大意: 给定01序列, 求随机交换k次后, 序列升序的概率. 假设一共$tot$个$0$, 设交换$i$次后前$tot$个数中有$j$个$0$的方案数为$dp[i][j]$, 答案即为$\frac{d ...
随机推荐
- 常用类一一MATH类一一两个静态常量PI 和E,一些数学函数。
package test; public class MathTest { public static void main(String[] args) { System.out.println(Ma ...
- destoon 分页
php: global $pagesize,$page; $pagesize = 10;//分页改为10条一页 $offset or $offset = ($page-1)*$pagesize; $t ...
- Centos7 配置ssh 免秘钥登陆
1.yum install -y openssh 2.servier1: ssh-keygen -t rsa #有提示的直接enter 3.server 2: ssh-keygen -t rsa # ...
- 日志记录发布网站之后不成功,对路径“C:\Inetpub\wwwroot\***\***.xls”的访问被拒绝。
主要是web程序的根目录文件夹路径访问权限不够,新增加一个everyone的完全控制读写的权限即可!---------折磨了两天,才发现使劲使错了地方. 另外: 一定谨记!!!!! 所写的路径如果不存 ...
- MD5摘要算法实现
网上找到的实现md5函数代码,包括一个头文件md5.h和一个源文件md5.c,用下面的测试代码test.c测试通过,各文件依次如下: 头文件md5.h: #ifndef MD5_H #define M ...
- np.random.random()系列函数
1.np.random.random()函数参数 np.random.random((1000, 20)) 上面这个就代表生成1000行 20列的浮点数,浮点数都是从0-1中随机. 2.numpy.r ...
- 在Linux下配置jdk的环境变量
jdk下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 在根目录新建s ...
- Dreamweaver 调字体大小
编辑--首选参数--字体 这里是指调代码字体的大小,就是为了编码时看的清楚些
- 在控制台远程连接mysql数据库时,出现ERROR 2049 (HY000)错误
问题的原因是,你本地的数据库版本过高,而远程的数据库版本低. 解决方法:在连接时加上 --skip-secure-auth 参数就可以了. mysql -h主机 -u用户名 -p密码 --skip- ...
- UI设计行业中的“延禧攻略”,教你从青铜变王者
最近一直在追<延禧攻略>,女主魏璎珞敢爱敢恨,有仇必报的性格吸引不少人,她从低贱的秀坊小宫女步步为营,最终成为皇帝最宠爱的令妃呼风唤雨.尔虞我诈的后宫,想要打怪升级光有颜值是万万不够的,更 ...