POJ 2151 Check the difficulty of problems 概率dp+01背包
题目链接:
http://poj.org/problem?id=2151
Check the difficulty of problems
Time Limit: 2000MSMemory Limit: 65536K
#### 问题描述
> Organizing a programming contest is not an easy job. To avoid making the problems too difficult, the organizer usually expect the contest result satisfy the following two terms:
> 1. All of the teams solve at least one problem.
> 2. The champion (One of those teams that solve the most problems) solves at least a certain number of problems.
>
> Now the organizer has studied out the contest problems, and through the result of preliminary contest, the organizer can estimate the probability that a certain team can successfully solve a certain problem.
>
> Given the number of contest problems M, the number of teams T, and the number of problems N that the organizer expect the champion solve at least. We also assume that team i solves problem j with the probability Pij (1
#### 输入
> The input consists of several test cases. The first line of each test case contains three integers M (0 输出
For each test case, please output the answer in a separate line. The result should be rounded to three digits after the decimal point.
样例输入
2 2 2
0.9 0.9
1 0.9
0 0 0
样例输出
0.972
题意
acm竞赛总共有m道题,t个队伍,并且知道第i个队伍做出第j道题的概率,求保证每个队伍都至少做出一道题,并且冠军队伍至少做出n道题的概率。
题解
原问题可以转换为求:所有的队伍都至少做出一题的概率-每个队伍都做出(1~n-1)道题的总概率。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-9;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=3333;
const int maxm=33;
///n个队伍,m道题,冠军至少做mm道。
int n,m,mm;
double dp[maxn][maxm];
double dp2[maxm][maxm];
double mat[maxn][maxm];
void init(){
clr(dp,0);
}
int main() {
while(scf("%d%d%d",&m,&n,&mm)==3&&n){
init();
for(int i=1;i<=n;i++){
clr(dp2,0);
///01背包,dp2[j][k],求第i个队伍前j道题做出k道的概率。
dp2[0][0]=1.0;
for(int j=1;j<=m;j++){
double x; scf("%lf",&x);
for(int k=0;k<=j;k++){
dp2[j][k]+=dp2[j-1][k]*(1-x);
if(k) dp2[j][k]+=dp2[j-1][k-1]*x;
}
}
///mat[i][k]:第i个队伍m道题做出k道的概率。
for(int k=0;k<=mm-1;k++) mat[i][k]=dp2[m][k];
}
///biger:所有的队伍都至少做出一题的概率
double biger=1.0;
for(int i=1;i<=n;i++) biger*=(1-mat[i][0]);
///dp[i][j]:统计前i个队伍中第i个队伍做了j题的总概率。
clr(dp,0);
for(int j=1;j<=mm-1;j++) dp[1][j]=mat[1][j];
for(int i=2;i<=n;i++){
for(int j=1;j<=mm-1;j++){
for(int k=1;k<=mm-1;k++){
dp[i][j]+=dp[i-1][k]*mat[i][j];
}
}
}
///sum:每个队伍都做出(1~mm-1)道题的总概率
double sum=0;
for(int i=1;i<=mm-1;i++) sum+=dp[n][i];
prf("%.3f\n",biger-sum);
}
return 0;
}
//end-----------------------------------------------------------------------
最新文章
- python2.7 报错(UnicodeDecodeError: &#39;ascii&#39; codec can&#39;t decode byte 0xe6 in position 0: ordinal not in range(128))
- lua中的数据类型
- 进制转换及API接口中的转换
- PyQT制作视频播放器
- [HTML]网页开发学习笔记
- C# 正则表达式类 Match类和Group类
- [数据结构]字典树(Tire树)
- JS字符串截取
- Gradle – Spring 4 MVC Hello World Example
- SQL中使用WITH AS提高性能
- (原+转)ubuntu16中莫名死机及重新安装显卡驱动
- C#映射网络驱动器
- java静态内部类理解
- android4.4之后的HttpUrlConnection的实现是基于okhttp
- centos 7 一键安装gitlab
- 解决Android Studio No cached version of org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.50 available for offline mode.
- 重写NLog
- Oracle性能优化5-索引的不足
- 查看项目中的laravel的版本
- linux-redhat-git源码安装
热门文章
- Lua 语言学习
- 生死系列--WuJie
- springboot-web进阶(二)——AOP统一处理请求
- 【转载】COM 组件设计与应用(二)——GUID 和 接口
- 远心镜头(Telecentric lens)原理介绍
- P2154 [SDOI2009]虔诚的墓主人
- Storm 第三章 Storm编程案例及Stream Grouping详解
- Spring 单元测试 RequestContextHolder.getRequestAttributes()).getRequest(); 为空的原因
- JavaScript验证时间格式
- 常见面试算法题JS实现-仅用递归函数和栈操作逆序一个栈