Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] E 三分
連結:戳這裡
E. Weakness and Poorness
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a sequence of n integers a1, a2, ..., an.Determine a real number x such that the weakness of the sequence a1 - x, a2 - x, ..., an - x is as small as possible.
The weakness of a sequence is defined as the maximum value of the poorness over all segments (contiguous subsequences) of a sequence.
The poorness of a segment is defined as the absolute value of sum of the elements of segment.
Input
The first line contains one integer n (1 ≤ n ≤ 200 000), the length of a sequence.
The second line contains n integers a1, a2, ..., an (|ai| ≤ 10 000).
Output
Output a real number denoting the minimum possible weakness of a1 - x, a2 - x, ..., an - x. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.
Examples
input
3
1 2 3
output
1.000000000000000
input
4
1 2 3 4
output
2.000000000000000
input
10
1 10 2 9 3 8 4 7 5 6
output
4.500000000000000
Note
For the first case, the optimal value of x is 2 so the sequence becomes - 1, 0, 1 and the max poorness occurs at the segment "-1" or segment "1". The poorness value (answer) equals to 1 in this case.
For the second sample the optimal value of x is 2.5 so the sequence becomes - 1.5, - 0.5, 0.5, 1.5 and the max poorness occurs on segment "-1.5 -0.5" or "0.5 1.5". The poorness value (answer) equals to 2 in this case.
題意:
給出n個數,找出一個實數x,使得weakness值最小
weakness:表示所有區間內的poorness的最大值
poorness:表示一段區間內的區間和的絕對值
思路:
由於是取絕對值的區間和,所以減去這個x使得值越大不行,越小也不行,肯定存在一個最優值使得它比x左邊和右邊都更優
三分這個極值,每次處理一下就可以了
至於怎麼處理這個絕對值最大問題,我們先-x按區間段最大找一遍,再把陣列去相反數找一次取max
第一次寫三分,好像直接for100次來的更簡單粗暴,毫無精度可言
程式碼:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n;
double a[200100],b[200100];
const double eps=1e-12;
double getmax(){
double ans=0,sum=0;
for(int i=1;i<=n;i++){
sum=sum+b[i];
if(sum<0) sum=0;
ans=max(ans,sum);
}
return ans;
}
double calc(double x){
for(int i=1;i<=n;i++) b[i]=a[i]-x;
double ans=getmax();
for(int i=1;i<=n;i++) b[i]=-b[i];
ans=max(ans,getmax());
return ans;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lf",&a[i]);
double l=-10000.0,r=10000.0,mid;
for(int i=0;i<100;i++){
double m1=l+(r-l)/3;
double m2=r-(r-l)/3;
if(calc(m1)<calc(m2)) r=m2;
else l=m1;
}
printf("%.12f\n",calc(l));
return 0;
}
相關文章
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] C 數學
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] (ABCDE題解)
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] D 字首字尾維護
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) E DFS
- Codeforces Round 892 (Div. 2) E
- Codeforces Round 936 (Div. 2) E
- Codeforces Round 945 (Div. 2) (A - E)
- Codeforces Round #846 (Div. 2) A-E
- Codeforces Round #851 (Div. 2) A-E
- Codeforces Round #283 (Div. 2) D,E
- Codeforces Round #681 (Div. 2)E題解
- Codeforces Round #361 (Div. 2) E 費馬小
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) A 模擬
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B 暴力
- Codeforces Round #401 (Div. 2)(C,D,E)
- Codeforces Round #316 (Div. 2) E dp
- Codeforces Round #288 (Div. 2) A,B,C,D,E
- Codeforces Round #287 (Div. 2)A,B,C,D,E
- Codeforces Round #280 (Div. 2 A,B,C,D,E)
- Educational Codeforces Round 168 (Rated for Div. 2) A - E
- Codeforces Round 976 (Div. 2) 題解(A-E)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) C 模擬
- Codeforces Round 938 (Div. 3) E
- Codeforces Round 981 (Div. 3)(A~E)
- Educational Codeforces Round 143 (Rated for Div. 2) A-E
- Codeforces Round #229 (Div. 2)(C,E待續)
- vp Educational Codeforces Round 168 (Rated for Div. 2) A - E
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) D 雙向dp
- Codeforces Round 975 (Div. 2)題解記錄(A,B,E)
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #678 (Div. 2)
- Codeforces Round #747 (Div. 2)
- Codeforces Round #673 (Div. 2)
- Codeforces Round #672 (Div. 2)
- Codeforces Round #448 (Div. 2) A
- Codeforces Round #217 (Div. 2)