码迷,mamicode.com
首页 > 其他好文 > 详细

URAL 1776 Anniversary Firework 概率dp+区间dp

时间:2016-05-07 11:03:56      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:

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


URAL 1776 Anniversary Firework 概率dp+区间dp

标签:

原文地址:http://blog.csdn.net/zp___waj/article/details/51334644

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!