CodeForces596D Wilbur and Trees

题目链接

一个人要去砍一排n棵树,他会等概率选取当前最左边或最右边的树,树有p的概率向左倒,1-p的概率向右倒,给出每棵树在数轴上的位置与树的高度,求树覆盖长度的期望

dp[l][r][lk][rk]表示区间[l,r]与l-1棵树倒的方向,r+1棵树倒的方向
更新方法比较好想,细节比较繁琐

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2005;
int h;
double p;
int a[maxn];
double dp[maxn][maxn][2][2];
double DW(int l,int r,int sl,int sr) 
{
	if (dp[l][r][sl][sr]) return dp[l][r][sl][sr];
	if (l==r)
	{
		if (sl&&a[l-1]+h>a[l])
		{
			if (sr) return dp[l][r][sl][sr]=min(h,a[l+1]-a[l]);
			else return dp[l][r][sl][sr]=min(h,max(0,a[r+1]-a[r]-h));
		}
		else if (!sr&&a[r+1]-h<a[r])
		{
			if (sl) return dp[l][r][sl][sr]=min(h,max(a[l]-a[l-1]-h,0));
			else  return dp[l][r][sl][sr]=min(h,a[l]-a[l-1]);
		}
		double ans=0;
		if (sl) ans+=p*min(h,max(a[l]-a[l-1]-h,0));
		else ans+=p*min(h,a[l]-a[l-1]);
		if (sr) ans+=(1-p)*min(h,a[r+1]-a[r]);
		else ans+=(1-p)*min(h,max(0,a[r+1]-a[r]-h));
		return dp[l][r][sl][sr]=ans;
	}
	if (sl&&a[l-1]+h>a[l])      return 
	dp[l][r][sl][sr]=DW(l+1,r,1,sr)+min(h,a[l+1]-a[l]);
	else if(!sr&&a[r+1]-h<a[r]) return
	dp[l][r][sl][sr]=DW(l,r-1,sl,0)+min(h,a[r]-a[r-1]);
	double ans=0;
	ans+=0.5*(1-p)*(DW(l+1,r,1,sr)+min(h,a[l+1]-a[l]));
	ans+=0.5*p    *(DW(l,r-1,sl,0)+min(h,a[r]-a[r-1]));
	if (sl) ans+=0.5*p*(DW(l+1,r,0,sr)+min(h,max(0,a[l]-a[l-1]-h)));
	else    ans+=0.5*p*(DW(l+1,r,0,sr)+min(h,a[l]-a[l-1]));
	if (sr) ans+=0.5*(1-p)*(DW(l,r-1,sl,1)+min(h,a[r+1]-a[r]));
	else    ans+=0.5*(1-p)*(DW(l,r-1,sl,1)+min(h,max(0,a[r+1]-a[r]-h)));
	return dp[l][r][sl][sr]=ans;
}
int main()
{
	int n;
	while (~scanf("%d%d%lf",&n,&h,&p))
	{
		for (int i=1;i<=n;i++) scanf("%d",&a[i]);
		memset(dp,0,sizeof(dp));
		sort(a+1,a+n+1);
		a[0]=a[1]-h;
		a[n+1]=a[n]+h;
		printf("%.12lf\n",DW(1,n,0,1));
	}
	return 0;
}

posted @ 2018-05-20 19:33  LJZ_C  阅读(162)  评论(0编辑  收藏  举报