標籤:
f(x)是個凹函數,三分即可,計算方案的時候dp一下。掛精度很坑,指定迴圈次數正解?
#include<bits/stdc++.h>using namespace std;const double eps = 1e-11;const int maxn = 2e5+5;double a[maxn];double d1[maxn],d2[maxn];int n;inline double cal(double x){ double a1 = 0., a2 = 0.; for(int i = 1; i <= n; i++){ d1[i] = max(0.,d1[i-1])+a[i]-x; a1 = max(a1,d1[i]); d2[i] = min(0.,d2[i-1])+a[i]-x; a2 = min(a2,d2[i]); } return max(fabs(a1),fabs(a2));}int main(){ //freopen("in.txt","r",stdin); scanf("%d",&n); for(int i = 1; i <= n; i++) scanf("%lf",a+i); double L = -1e4, R = 1e4; double M1v,M2v; while(R-L>eps){ double M1 = L+(R-L)/3, M2 = L+(R-L)/3*2; M1v = cal(M1), M2v = cal(M2); if(abs(M1v-M2v)<eps){ L = M1; R = M2; }else { if(M1v > M2v){ L = M1; }else { R = M2; } } } printf("%.15lf",(cal(L)+cal(R))/2); return 0;}
Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] C A Weakness and Poorness