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 ...
随机推荐
- sessionpage1
session1 <%@page import="java.text.SimpleDateFormat"%> <%@ page language="ja ...
- 在SQL Server中使用CLR调用.NET方法
介绍 我们一起来做个示例,在.NET中新建一个类,并在这个类里新建一个方法,然后在SQL Server中调用这个方法.按照微软所述,通过宿主 Microsoft .NET Framework 2 ...
- 脱离SVN的控制
在桌面创建一个记事本文件,然后吧这句话复制进去for /r . %%a in (.) do @if exist "%%a\.svn" rd /s /q "%%a\.svn ...
- oracle 查看被锁的表和解锁
相关视图 SELECT * FROM v$lock;SELECT * FROM v$sqlarea;SELECT * FROM v$session;SELECT * FROM v$process ;S ...
- Alpha Level (Significance Level)
1.Alpha Level (Significance Level,显著水平): What is it? 显著性水平α是指当零假设是正确的,但做出了错误决策的概率(即一类错误的概率).Alpha水平( ...
- 问题解决Android studio遇到 java.lang.OutOfMemoryError: GC app:transformClassesWithDexForDebug解决方法 以及gradle优化
http://blog.csdn.net/xiaoxing0828/article/details/52242090
- express.Router
[express.Router] 1.可使用 express.Router 类创建模块化.可挂载的路由句柄.Router 实例是一个完整的中间件和路由系统,因此常称其为一个 “mini-app”. 下 ...
- EasyUI多选的获取
function deletePRE() { var rows = $('#dg').datagrid('getSelections'); var ids = []; var other_ids = ...
- Java.sql.SQLException: 无效的列类型: 1111
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: ...
- js join 与 split
var a = [] var b = [1,2,3] b.push('4') // b = [1,2,3,4] a = b.join('-') // a = '1-2-3-4' b = a.sp ...