UVa 10529 - Dumb Bones

分析和提示可以看我的一篇总结性文章 UVa 10529


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <deque>
#include <algorithm>
#include <string>

using namespace std;
const int maxn = 1100;

double pl , pr;

double d[maxn];
int vis[maxn] , k[maxn];

double dp(int n)
{
	if(vis[n]) return d[n];
	vis[n] = 1;
	double& res = d[n] = 1e10;
	k[n] = 1;
	if(n==0) return res = 0;
	
	for(int i=k[n-1];i<=n;i++)
		if(res > (1-pr)/(1-pl-pr)*dp(i-1)+(1-pl)/(1-pl-pr)*dp(n-i)+1/(1-pl-pr)) res = (1-pr)/(1-pl-pr)*dp(i-1)+(1-pl)/(1-pl-pr)*dp(n-i)+1/(1-pl-pr) , k[n] = i;
	return res;
}

int main()
{
	freopen("in","r",stdin);
	int n;
	while(cin>>n && n)
	{
		memset(vis , 0 , sizeof(vis));
		cin>>pl>>pr;
		for(int i=0;i<=n;i++) dp(i);
		printf("%.2lf\n", dp(n));
	}
	
	return 0;
}


你可能感兴趣的:(概率,uva)