标签:bsp printf less for and case tin void pos
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 const int maxn = 1100; 5 int n; 6 double l,r; 7 double dp[maxn]; 8 double calc (int n,int x) 9 { 10 return (1.0+l*dp[x-1]+r*dp[n-x])/(1-l-r)+dp[x-1]+dp[n-x]; 11 } 12 int main() 13 { 14 //freopen("de.txt","r",stdin); 15 while (~scanf("%d",&n)){ 16 if (n==0) break; 17 scanf("%lf%lf",&l,&r); 18 dp[0]=0; 19 dp[1]=1.0/(1-r-l); 20 for (int i=2;i<=n;++i){ 21 dp[i]=calc(i,i); 22 for (int j=1;j<=i;++j) 23 dp[i]=min(dp[i],calc(i,j)); 24 } 25 printf("%.2f\n",dp[n]); 26 } 27 return 0; 28 }
优化成O(n)
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 double f[100005]; 5 double pl,pr,ans; 6 int n; 7 inline double cal(int n,int i){ 8 return (pl*f[i-1]+pr*f[n-i]+1)/(1.0-pl-pr)+f[i-1]+f[n-i];//计算期望 9 } 10 int main(){ 11 cin>>n; 12 int i,j,x=1; 13 cin>>pl>>pr; 14 f[0]=0;f[1]=1.0/(1.0-pl-pr);//初始化,0张牌期望为0,一张牌期望为既不左倒也不右倒的概率 15 for(i=2;i<=n;i++){ 16 f[i]=cal(i,x); 17 while(x<i&&cal(i,x+1)<f[i]){f[i]=cal(i,x+1);x++;}//更优的决策点一定在上一次决策点之后 18 } 19 printf("%.2lf",f[n]);//保留两位 20 return 0; 21 }
标签:bsp printf less for and case tin void pos
原文地址:http://www.cnblogs.com/agenthtb/p/7487788.html