【牛客】练习赛26 - B烟花(概率DP)

题目链接

题意:小a有个烟花,每个烟花代表着互不相同的颜色,对于第个烟花,它有的概率点燃,现在小a要去点燃它们,他想知道产生颜色的期望个数 及 产生恰好产生种颜色的概率 。

思路:概率DP 。
我们先看下面这个表格,是n为3,k为2的情况。

i\dp\j012
0100
1 1p1 1 − p 1 p1 p 1 0
2 (1p1)(1p2) ( 1 − p 1 ) ∗ ( 1 − p 2 ) (1p1)p2+p1(1p2) ( 1 − p 1 ) ∗ p 2 + p 1 ∗ ( 1 − p 2 ) p1p2 p 1 ∗ p 2
3 (1p1)(1p2)(1p3) ( 1 − p 1 ) ∗ ( 1 − p 2 ) ∗ ( 1 − p 3 ) (1p1)(1p2)p3+(1p1)p2(1p3)+p1(1p2)p3 ( 1 − p 1 ) ∗ ( 1 − p 2 ) ∗ p 3 + ( 1 − p 1 ) ∗ p 2 ∗ ( 1 − p 3 ) + p 1 ∗ ( 1 − p 2 ) ∗ p 3 (1p1)p2p3+p1(1p2)p3+p1p2(1p3) ( 1 − p 1 ) ∗ p 2 ∗ p 3 + p 1 ∗ ( 1 − p 2 ) ∗ p 3 + p 1 ∗ p 2 ∗ ( 1 − p 3 )

可以看出来对于前i个烟花,恰好有j个燃放的概率只与前i-1个烟花,恰好有j个或j-1个燃放的概率有关。

设DP[i][j]表示前i个烟花恰好爆炸了j个的概率,可得DP方程:

{DP[i][j]=DP[i1][j](1pi)+DP[i1][j1]piDP[0][0]=1 { D P [ i ] [ j ] = D P [ i − 1 ] [ j ] ∗ ( 1 − p i ) + D P [ i − 1 ] [ j − 1 ] ∗ p i D P [ 0 ] [ 0 ] = 1

AC代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
typedef long long ll;
typedef pair<int, int> pii;
inline int read(){int r=0;char c=getchar();while(c<'0'||c>'9') {c=getchar();}while(c>='0'&&c<='9') {r=r*10+c-'0';c=getchar();}return r;}
inline ll readll(){ll r=0;char c=getchar();while(c<'0'||c>'9') {c=getchar();}while(c>='0'&&c<='9') {r=r*10+c-'0';c=getchar();}return r;}
const double eps = 1e-8;
const double PI = acos(-1.0);
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
const int MAXN = 1e5;
const int MAXM = 1e5;

double dp[MAXN][233];
int main(int argc, char const *argv[])
{
    int n, k;
    double p, ans = 0;
    cin >> n >> k;
    dp[0][0] = 1;
    for(int i=1; i<=n; ++i){
        cin >> p;
        ans += p;
        dp[i][0] = dp[i-1][0]*(1.0-p);
        for(int j=1; j<=k;++j){
            dp[i][j] = dp[i-1][j]*(1.0-p)+dp[i-1][j-1]*p;
        }
    }
    printf("%.4lf\n%.4lf", ans, dp[n][k]);
    return 0;
}

这个DP方程的时间复杂度已经无法优化了,但是空间复杂度还可以从n*k优化成k。
我们用DP[i]表示在已经燃放了一下烟花后,恰好燃放了i个烟花的概率,重复利用DP数组。

但是有一点需要注意:我们更新DP时,需要从后向前更新,因为从前向后更新会将后面更新要用到的上一层的DP值覆盖。

AC代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
typedef long long ll;
typedef pair<int, int> pii;
inline int read(){int r=0;char c=getchar();while(c<'0'||c>'9') {c=getchar();}while(c>='0'&&c<='9') {r=r*10+c-'0';c=getchar();}return r;}
inline ll readll(){ll r=0;char c=getchar();while(c<'0'||c>'9') {c=getchar();}while(c>='0'&&c<='9') {r=r*10+c-'0';c=getchar();}return r;}
const double eps = 1e-8;
//const double PI = acos(-1.0);
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
const int MAXN = 1e5;
const int MAXM = 1e5;

double dp[233];
int main(int argc, char const *argv[])
{
    int n, k;
    double p, ans = 0;
    cin >> n >> k;
    dp[0] = 1;
    for(int i=1; i<=n; ++i){
        cin >> p;
        ans += p;
        for(int j=k; j>=0; --j){
            dp[j] = dp[j]*(1.0-p)+dp[j-1]*p;
        }
    }
    printf("%.4lf\n%.4lf", ans, dp[k]);
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值