「Codeforces」596D (dfs+区间dp+细节)

题意:原题在这

Wilbur the pig really wants to be a beaver, so he decided today to pretend he is a beaver and bite at trees to cut them down.

There are nn trees located at various positions on a line. Tree ii is located at position x_{i}xi . All the given positions of the trees are distinct.

The trees are equal, i.e. each tree has height hh . Due to the wind, when a tree is cut down, it either falls left with probability pp , or falls right with probability 1-p1p . If a tree hits another tree while falling, that tree will fall in the same direction as the tree that hit it. A tree can hit another tree only if the distance between them is strictly less than hh .

For example, imagine there are 44 trees located at positions 11 , 33 , 55and 88 , while h=3h=3 and the tree at position 11 falls right. It hits the tree at position 33 and it starts to fall too. In it's turn it hits the tree at position 55 and it also starts to fall. The distance between 88 and 55is exactly 33 , so the tree at position 88 will not fall.

As long as there are still trees standing, Wilbur will select either the leftmost standing tree with probability 0.50.5 or the rightmost standing tree with probability 0.50.5 . Selected tree is then cut down. If there is only one tree remaining, Wilbur always selects it. As the ground is covered with grass, Wilbur wants to know the expected total length of the ground covered with fallen trees after he cuts them all down because he is concerned about his grass-eating cow friends. Please help Wilbur.

给定树的数量,高度和位置,每棵树有p概率向左倒,1-p向右倒

小sb会选择这一排树的端点砍倒

求出倒下的树所覆盖的地面长度

做法:(详见行内注释)

1. 递归预处理每棵树向左/右倒压倒的最大长度

2. dp[l][r][0/1][0/1]表示从l到r区间l-1、r+1向左/右的期望

情况:
  (1).最左边向左倒和最右边向右倒,均不影响,取min计算重叠
  (2).计算向区间中央倒

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define inf 999999999
#define maxn 100000005
using namespace std;

int n;
int pos[2005],maxl[2005],maxr[2005];
double p,h;
double dp[2005][2005][2][2];//dp[l][r][0/1][0/1]表示从l到r区间l-1、r+1向左/右的期望

void pre()//递归预处理每棵树向左/右倒压倒的最大长度
{
    maxl[1]=1;
    for(int i=2;i<=n;i++)
    {
        if(pos[i]-pos[i-1]<h) maxl[i]=maxl[i-1];
        else maxl[i]=i;
    }
    maxr[n]=n;
    for(int i=n-1;i>=1;i--)
    {
        if(pos[i+1]-pos[i]<h) maxr[i]=maxr[i+1];
        else maxr[i]=i;
    }
}
/*

*/
double dfs(int l,int r,int sl,int sr)
{
    //边界处理
    if(l>r) return 0;
    if(dp[l][r][sl][sr]!=0) return dp[l][r][sl][sr];//记忆化搜索,dp一算到底
    
    //计算端点
    double temp=dp[l][r][sl][sr];
    //最左边向左倒和最右边向右倒,均不影响,取min计算重叠
    temp+=0.5*p*(min(pos[l]-pos[l-1]-sl*h,h)+dfs(l+1,r,0,sr));
    temp+=0.5*(1-p)*(min(pos[r+1]-pos[r]-sr*h,h)+dfs(l,r-1,sl,0));
    //计算向区间中央倒
    if(maxr[l]>=r) temp+=0.5*(1-p)*(min(pos[r+1]-pos[r]-sr*h,h)+pos[r]-pos[l]);
    else temp+=0.5*(1-p)*(pos[maxr[l]]-pos[l]+h+dfs(maxr[l]+1,r,1,sr));

    if(maxl[r]<=l) temp+=0.5*p*(min(pos[l]-pos[l-1]-sl*h,h)+pos[r]-pos[l]);
    else temp+=0.5*p*(pos[r]-pos[maxl[r]]+h+dfs(l,maxl[r]-1,sl,1));

    return dp[l][r][sl][sr]=temp;
}

/*  9 2 0.800000
    3 5 7 9 11 13 15 17 18
    */
int main()
{
    cin>>n>>h>>p;
    for(int i=1;i<=n;i++) cin>>pos[i];
    sort(pos+1,pos+n+1);
    pos[0]=-inf;pos[n+1]=inf;
    pre();
    memset(dp,0,sizeof(dp));
    printf("%.20lf
",dfs(1,n,0,0));
    return 0;
}
原文地址:https://www.cnblogs.com/LocaEtric/p/9614945.html