Codeforces #28 C.Bath Queue (概率dp)
2024-08-31 18:25:30
Codeforces Beta Round #28 (Codeforces format)
题目链接: http://codeforces.com/contest/28/problem/C
题意:
有 \(n\) 个人,\(m\) 间浴室,每间浴室有\(a[ i ]\)个浴缸,每个人要洗澡的话都要排队,假如一群人进入同一个浴室,他们总倾向于使得最长的队伍最短,现在问你所有队伍中最长的期望?
中文题解:
用状态 \(dp[i][j][k]\) 表示还剩 \(i\) 间浴室,还剩 \(j\) 个人,之前最长队伍的长度为 \(k\) 的期望最长队伍长度。
那么状态转移方程为:
$dp[i][j][k] = \sum_{i=1}{m}\sum_{j=0}{n}\sum_{k=1}{n}\sum_{c=1}{j}(dp[i-1][j-c][max(k, \frac{c+a[i]-1}{a[i]})] * \frac{(i-1)^{j-c}}{ i^j } * C(j, c)) $
其中 \(c\) 是枚举当前去第 \(j\) 间浴室的人数。
那么答案就是 \(dp[m][n][0]\) 。
时间复杂度:\(O(n^{3}*m)\)
英文题解:
This problem is solved by dynamic programming
Consider the following dynamics: \(dp[i][j][k]\).
\(i\) --- number of not yet processed students,
\(j\) --- number of not yet processed rooms,
\(k\) --- maximum queue in the previous rooms.
The value we need is in state \(dp[n][m][0]\). Let's consider some state \((i, j, k)\) and search through all \(c\) from 0 to \(i\). If \(c\) students will go to \(j\)th room, than a probability of such event consists of factors: \(C_{i}^{c}\) --- which students will go to \(j\)th room.
\((1 / j)^c· ((j - 1) / j)^{i-c}\) --- probability, that \(c\) students will go to \(j\)th room,and the rest of them will go to the rooms from first to \(j - 1\)th.
Sum for all \(ñ\) from 0 to \(i\) values of
\((1 / j)^c· ((j - 1) / j)^{i-c}·C_{i}^{c}· dp[i-c][j-1][mx]\) . Do not forget to update maximum queue value and get the accepted.
代码:
#include<bits/stdc++.h>
#pragma GCC optimize ("O3")
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
int n,m;
int a[56];
double dp[56][56][56];
double C[56][56];
//题解:
//http://www.cnblogs.com/LzyRapx/p/7692702.html
int main()
{
cin>>n>>m;
C[0][0] = 1.0;
for(int i=1;i<=55;i++)
{
C[i][0] = 1.0;
for(int j=1;j<=i;j++)
{
C[i][j] = C[i-1][j-1] + C[i-1][j];
}
}
for(int i=1;i<=m;i++) cin>>a[i];
for(int i=0;i<=n;i++) dp[0][0][i] = i;
for(int i=1;i<=m;i++)
{
for(int j=0;j<=n;j++)
{
for(int k=0;k<=n;k++)
{
for(int c=0;c<=j;c++)
{
int Max = max(k,(c+a[i]-1)/a[i]);
dp[i][j][k] += dp[i-1][j-c][Max] * pow(i-1,j-c) / pow(i,j) * C[j][c];
}
}
}
}
printf("%.10f\n",dp[m][n][0]);
return 0;
}
最新文章
- 使用EF取数据库返回的数据
- WebApi 通过类名获取类并实例化
- expr命令的一些用法
- HTTPS基本原理
- ubuntu 安装 GCC
- HTML 参考手册
- Xaml/Xml 实现对象与存储分离
- CRM IFRAME 显示地图
- DOS下更改编码方式
- 在mac本上折腾android 开发环境
- 27.编写一个Animal类,具有属性:种类;具有功能:吃、睡。定义其子类Fish 和Dog,定义主类E,在其main方法中分别创建其对象并测试对象的特性。
- 我想操作的是利用SqlDataAdapter的几个Command属性(InsertCommand,UpdateCommand,DeleteCommand)来更新数据库
- Linux系统开机启动流程
- 5.spark弹性分布式数据集
- scala时间处理
- ed命令
- GNU MAKE参考文档
- group by having 判断重复的有几条数据
- BZOJ.2095.[POI2010]Bridges(最大流ISAP 二分 欧拉回路)
- iis6-0 cve-2017-7269 批量验证脚本