
题目地址
题解
二分答案,设为$ans$,那么判定条件
移项,等价于
求右边的最大值即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <cstdlib> #include <algorithm> #include <cstring> #include <cctype> #define INF 1000000000 using namespace std; typedef long long ll; int (){ int f=1,x=0;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-f;c=getchar();} while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar(); return f*x; } int n,a[100005]; double eps=1e-4,sum[100005]={0}; bool C(double ans){ double mini=sum[1]-ans,maxi=sum[2]-2*ans-(sum[1]-ans); for(int i=2;i<n;i++){ maxi=max(maxi,sum[i]-(double)i*ans-mini); mini=min(mini,sum[i]-(double)i*ans); } return (sum[n]-(double)n*ans<=maxi); } void init(){ n=read(); for(int i=1;i<=n;i++) a[i]=read(),sum[i]=sum[i-1]+a[i]; } void solve(){ double L=0,R=(a[1]+a[n])*0.5,M; while(R-L>eps){ M=(R+L)*0.5; if(C(M))R=M;else L=M; } printf("%.3lfn",L); } int main(){ init(); solve(); return 0; }
|
近期评论