Gym - 101190F 期望DP

题目链接:https://vjudge.net/problem/Gym-101190F

 

解题思路:

考虑每张照片在顶部的概率然后乘以这个点的期望长度,将这个值记做e[i],那么最后答案就是∑e[i].

 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx = 1e6 + 10;
double dp[mx];
char s[mx];
int pre[mx];
ll w[mx],c[mx];
int main(){
	freopen("foreign.in","r",stdin);
	freopen("foreign.out","w",stdout);
	scanf("%s",s+1);
	int n = strlen(s+1); 
	for(int i=1;i<=n;i++){
		pre[i] = pre[i-1];
		if(s[i]=='W') pre[i]++;
	}
	double ans = 0,d = 1.0/n;
	for(int i=1;i<=n;i++) w[i] = w[i-1] + pre[i];
	for(int i=1;i<=n;i++) c[i] = c[i-1] + (i-pre[i]);
	if(s[1]=='C') ans = 1.0*w[n] / n;
	else ans = 1.0*c[n] / n;
	ll ret;
	for(int i=2;i<=n;i++){
		int l = n-i+1;
		if(s[i]=='C') ret = w[n] - w[i-1] - 1ll*l*pre[i-1];
		else ret = c[n] - c[i-1] - 1ll*l*(i-1-pre[i-1]);
		ans += d*ret/l;
		d = d + d/l; 
	}
	printf("%.12lf\n",ans);
	return 0;
} 

 

上一篇:G - Green-Red Tree Gym - 102190G


下一篇:AI与Python - 强化学习