URAL 1776 Anniversary Firework 概率dp+区间dp

A - Anniversary Firework
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  URAL 1776
Appoint description:  System Crawler  (2016-05-06)

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;
}


你可能感兴趣的:(dp,ACM)