URAL 1776 Anniversary Firework 概率dp+区间dp

本文介绍了一个关于火箭庆典的算法问题,通过动态规划方法解决了给定数量的火箭按特定规则发射时,平均等待时间的计算问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A - Anniversary Firework
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Appoint description: 

Description

Denis has to prepare the Ural State University 90th anniversary firework. He bought  n rockets and started to think of the way he should launch them. After a pair of sleepless nights he invented the following algorithm.
All  n rockets are placed on the surface in a single line. The interval between two consecutive salvos is ten seconds. The leftmost and the rightmost rocket are launched in the first salvo. After  i salvos are fired, all non-empty segments between two neighboring launched rockets are considered. One rocket is chosen randomly and uniformly at each of these segments. All chosen rockets are launched in the ( i + 1)-st salvo. Algorithm runs until all rockets are launched.
Calculate the average duration in seconds of such a firework.

Input

The only input line contains an integer  n  (3 ≤  n ≤ 400)  , which is the number of rockets bought by Denis.

Output

Output the expected duration of the firework in seconds, with absolute or relative error not exceeding 10  −6.

Sample Input

input output
5
26.66666666666

Notes

First, the rockets with numbers 1 and 5 are launched. 10 seconds later the rocket 3 is launched with probability 1/3; in that case, 10 more seconds later the rockets 2 and 4 are launched, and the firework is over after 20 seconds. In case the rocket 2 or rocket 4 is launched in the second salvo (this happens with probability 2/3), the firework is over after 30 seconds.


题目的意思给你n个火箭排成一排

一开始点燃第一个和最后一个火箭

然后每次只能在点燃过的火箭中的火箭

每两次点燃火箭的间隔时间为10s求

点燃n个火箭等待时间的期望



设dp【i,j】表示i个火箭等待了j次

那么求花费j次的概率为 dp[【i,j】=dp【i,j-1】

然后就枚举长度和次数

ACcode:

#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 404
using namespace std;
double dp[maxn][maxn];
int main(){
    int n;
    while(~scanf("%d",&n)){
        n-=2;
        for(int i=0;i<=n;++i)for(int j=i;j<=n;++j)dp[i][j]=1.0;
        for(int i=1;i<=n;i++){
            double e=1.0/i;
            for(int j=2;j<i; j++){
                dp[i][j]=dp[i][j-1];
                for(int k=1;k<=i;k++){
                    int l=k-1,r=i-k;
                    double p1=dp[l][j-1],p2=dp[r][j-1],p3=dp[l][j-2],p4=dp[r][j-2];
                    dp[i][j]+=e*(p1*p2-p3*p4);
                }
            }
        }
        double ans = 0;
        for(int i = 1; i <= n; i++){
            ans += (dp[n][i]-dp[n][i-1])*i*10;
        }
        printf("%.11lf\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值