CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B
题目大意:给定n个数a1…an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a3…+ai,问满足Si<=p的i的最大值的期望.(p<=50)
(大意来自于http://www.cnblogs.com/liu-runda/p/6253569.html)
我们知道,全排列其实等价于我们一个一个地等概率地向一个序列里面插入数值
所以我们可以这么看这道题:
现在有n个数,有n个盒子,等概率地把这些数放入盒子,求Si<=p的max[i]的期望
我们考虑每个盒子对于答案的贡献
如果第一个盒子保存下来了,那么它的贡献就是p(保存下来的概率)
所以我们只需要计算每个盒子保存下来的概率就可以了
我们又发现:如果第i个盒子保存下来了,那么1~(i-1)的盒子一定都保存下来了
不然第i个盒子就不可能保存下来,原文中:
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.
起到了关键作用
所以我们定义f[i][j][k]表示在前i个数中随机的把数放到盒子里
前j个盒子被放满且前j个盒子中的数之和为k的情况出现的概率
那么我们有f[i][j][k] = f[i-1][j][k]*(1 - (j/i)) + f[i-1][j-1][k - a[i]]*(j/i)
则ans = sigma{f[n][i][j]}(1<=i<=n;0<=j<=p)
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=*x+ch-'',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = ;
double f[][maxn][maxn];
int a[maxn];
int main(){
int n;read(n);
for(int i=;i<=n;++i) read(a[i]);
int p;read(p);
f[][][] = 1.0;
for(int i=;i<=n;++i){
for(int j=;j<=i;++j){
for(int k=;k<=p;++k){
if(a[i] > k) f[i&][j][k] = f[(i-)&][j][k]*(i-j)/i;
else f[i&][j][k] = f[(i-)&][j][k]*(i-j)/i + f[(i-)&][j-][k-a[i]]*j/i;
}
}
}
double ans = .;
for(int i=;i<=n;++i){
for(int j=;j<=p;++j){
ans += f[n&][i][j];
}
}printf("%.10lf\n",ans);
getchar();getchar();
return ;
}
CodeForces - 261B Maxim and Restaurant的更多相关文章
- codeforces 261B Maxim and Restaurant(概率DP)
B. Maxim and Restaurant time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- CodeForces 261B Maxim and Restaurant 解法汇总
题意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a3-+ai,问满足Si<=p的i的最大值的期望.(p<=50) 这道题在网上有一些不同 ...
- cf 261B.Maxim and Restaurant
什么什么期望的,不会! (题解http://blog.sina.com.cn/s/blog_140e100580102wj4e.html(看不懂)) #include<bits/stdc++.h ...
- 【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 Round #160 (Div. 2) D. Maxim and Restaurant(DP)
题目链接 想了挺久,枚举每一件物品,当做关键物品,假设再加这一件物品,就>=c了,把剩下的物品背一下包,dp[i][j]表示i个物品可以组成重量j的个数. 这样就可以知道前面放i件,后边肯定放n ...
- Codeforces Round #423 A Restaurant Tables(模拟)
A. Restaurant Tables time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces 854B Maxim Buys an Apartment:贪心
题目链接:http://codeforces.com/contest/854/problem/B 题意: 有n栋房子从1到n排成一排,有k栋房子已经被售出. 现在你要买一栋“好房子”. 一栋房子是“好 ...
- Codeforces F. Maxim and Array(构造贪心)
题目描述: Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- CodeForces 721D Maxim and Array
贪心,优先队列. 先看一下输入的数组乘积是正的还是负的. ①如果是负的,也就是接下来的操作肯定是让正的加大,负的减小.每次寻找一个绝对值最小的数操作就可以了. ②如果是正的,也是考虑绝对值,先操作绝对 ...
随机推荐
- CString转换为LPSTR和LPSTR转化为CString
一.CString转换为LPSTR 方法一: CString strFileName LPSTR lpstr - strFileName.GetBuffer(); strFileName.Releas ...
- jQuery获取短信验证码+倒计时实现
jQuery 短信验证码倒计时 <script type="text/javascript" charset="utf-8"> $(function ...
- echarts学习总结
ECharts学习总结使用步骤1 引入ECharts <script src="echarts.min.js"></script>2 绘制一个简单的图表 为 ...
- Webmin 安装 (centos7 rpm 方式)
网上有很多此类的教程,大多都很老了.这里记录下自己安装Webmin的过程. # 系统准备 > yum -y install perl perl-Net-SSLeay openssl perl-I ...
- SharePoint 2013 通过JavaScript实现列表标题列宽度可拖动
前言 最近有个新需求,用户希望标题栏可以拖动宽度,其实觉得没什么用,既然用户要了又推不掉,就勉为其难实现一下吧. 其实原理比较简单,就是利用JavaScript对标题栏进行宽度控制,然后从网上搜了一下 ...
- Angular版本1.2.4在IE11的IE8模式下出错解决方案
今天,群里一个兄弟抛出一个问题(如上),截图说明. 打断点调试下,貌似是console里面的log方法出错了,如下: 这个是console的log方法,为什么错呢,继续: 我们会发现,在这里是检测fu ...
- iOS类中的属性设置背景色(统一)
unsigned int count; objc_property_t *properties = class_copyPropertyList([self class], &count); ...
- 【代码笔记】iOS-由身份证号码返回性别
一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. ...
- linux Mysql 安装及配置
1.准备 cmake-3.6.0.tar.gz bison-3.0.4.tar.gz mysql-5.7.13.tar.gz (http://dev.mysql.com/get/Downloads/M ...
- JavaScript & PHP模仿C#中string.format效果
1.JavaScript function stringformat() { var args = Array.prototype.slice.call(arguments); if (args.le ...