解题思路
从后往前考虑,令dp[i] 为从i-n这段区间可以切成的期望段数,从后往前就比较好转移了。
dp[i]=1j−i+1∑k=i+1j+1dp[k]+1
AC代码
/*
* @Author: wchhlbt
* @Last Modified time: 2017-11-17
*/
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define AA first
#define BB second
#define ONES(x) __builtin_popcount(x)
#define _ << " " <<
using namespace std;
typedef pair<int, int> P;
typedef long long ll ;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
const double eps =1e-3;
const int mod = 1000000007;
const double PI = acos(-1.0);
inline int read(){ int num; scanf("%d",&num); return num;}
const int maxn = 100020;
int a[maxn];
double dp[maxn];// dp[i] i-n 可以切成的期望段数
double dps[maxn];// 维护一个dp数组的后缀和
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int mx = 0;
for(int i = 1; i<=n; i++) a[i] = read(), mx = max(mx,a[i]), a[i] += a[i-1];
while(m--){
int x = read();
if(x<mx){ //小于最大值的情况下无解
puts("YNOI is good OI!");
continue;
}
memset(dp,0,sizeof dp);
memset(dps,0,sizeof dps);
dp[n] = dps[n] = 1;
for(int i = n-1, j = n; i>=1; i--){
while(a[j]-a[i-1]>x) j--; //对每个i找一个j 让 i-j的和 ≤ x
dp[i] = 1 + 1.0/(j-i+1)*(dps[i+1]-dps[j+2]);
dps[i] = dp[i] + dps[i+1];
}
printf("%.2f\n",dp[1]);
//cout << dp[1] << endl;
}
return 0;
}