题意 : 题目链接
分析 : 听队友说一般概率从前往后推、期望从后往前推.........
#include<bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; double dp[maxn], sumdp[maxn]; int sum[maxn], arr[maxn]; int MaxEle; int main(void) { int n, m; while(~scanf("%d %d", &n, &m)){ sum[0] = 0; for(int i=1; i<=n; i++){ scanf("%d", &arr[i]); sum[i] = sum[i-1] + arr[i]; dp[i] = 0; } MaxEle = *max_element(arr+1, arr+1+n); int x; while(m--){ scanf("%d", &x); if(x < MaxEle) { puts("YNOI is good OI!"); continue; } dp[n] = sumdp[n] = 1; sumdp[n+1] = 0; for(int j=n, i=n-1; i>=1; i--){ while(sum[j] - sum[i-1] > x) j--; ///判断 sum(i~j)是否小于x dp[i] = 1.0/(j-i+1.0) * (sumdp[i+1] - sumdp[j+2]/*注意这里是后缀和*/) + 1.0; sumdp[i] = sumdp[i+1] + dp[i]; } printf("%.2lf ", dp[1]); } } return 0; }