text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
int n, arr[200002];
void Input() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
}
}
long double eabs(long double num) {
if (num < 0) return -num;
return num;
}
long double Evaluate(long double x) {
long double res = 0.0, curmin = 0.0, curmax = 0.0, cursum = 0.0;
for (int i = 1; i <= n; i++) {
long double val = (long double)arr[i] - x;
cursum += val;
curmin = min(curmin, cursum);
curmax = max(curmax, cursum);
res = max(res, eabs(cursum - curmin));
res = max(res, eabs(cursum - curmax));
}
return res;
}
long double eps = 0.0000000000001;
bool Equal(long double a, long double b) { return (eabs(a - b) < eps); }
int main() {
long double bl, br, bmid, bres = -1.0;
bl = -10000.0;
br = 10000.0;
Input();
while (bl < br || (Equal(bl, br))) {
bmid = (bl + br) / 2.0;
long double prev = Evaluate(bmid - eps), cur = Evaluate(bmid);
if (bres < 0) bres = cur;
bres = min(bres, cur);
if (prev < cur) {
br = bmid - eps;
} else {
bl = bmid + eps;
}
}
printf("%.7f\n", (double)bres);
}
|
#include <bits/stdc++.h>
using namespace std;
int a[200005];
long double b[200005];
int n;
long double dp1[200005], dp2[200005];
long double check(long double val) {
int i;
for (i = 0; i < n; i++) b[i + 1] = a[i] - val;
dp1[1] = b[1];
long double ans = b[1];
for (i = 2; i <= n; i++) {
dp1[i] = max(dp1[i - 1], (long double)0) + b[i];
ans = max(ans, dp1[i]);
}
for (i = 0; i <= n + 1; i++) b[i + 1] = -b[i + 1];
ans = max(ans, b[1]);
dp2[1] = b[1];
for (i = 2; i <= n; i++) {
dp2[i] = max(dp2[i - 1], (long double)0) + b[i];
ans = max(ans, dp2[i]);
}
return ans;
}
int main() {
scanf("%d", &n);
int i;
for (i = 0; i < n; i++) scanf("%d", &a[i]);
long double lo, hi;
lo = -(1e4);
hi = 1e4;
long double eps = 1e-12;
while (lo < hi - eps) {
long double mid1, mid2;
mid1 = (2 * lo + hi) / 3;
mid2 = (lo + hi * 2) / 3;
long double sol1, sol2;
sol1 = check(mid1);
sol2 = check(mid2);
if (sol1 < sol2 - eps) {
hi = mid2;
} else {
lo = mid1;
}
}
cout << fixed << setprecision(9) << check(lo) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, a[200001];
long double b[200001], pre[200001], ans = 2e11;
long double check(long double x) {
for (int i = 1; i <= n; i++) b[i] = a[i] - x;
for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + b[i];
long double mn = 2e13, mx = -2e13;
long double mns = 0, mxs = 0;
for (int i = 1; i <= n; i++) {
mn = min(mn, pre[i] - mxs);
mx = max(mx, pre[i] - mns);
mns = min(mns, pre[i]);
mxs = max(mxs, pre[i]);
}
return max(-mn, mx);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
long double low = -1e4, high = 1e4;
for (int i = 0; i <= 120; i++) {
long double med = (low + high) / 2;
long double x = check(med);
long double y = check(med + 1e-11);
ans = min(ans, min(x, y));
if (x < y)
high = med;
else
low = med + 1e-11;
}
cout << fixed << setprecision(8) << ans << '\n';
}
|
#include <bits/stdc++.h>
#pragma GCC target("avx,avx2,fma")
using namespace std;
template <typename T>
int len(const T& a) {
return a.size();
}
using ll = long long;
constexpr int N = 2e5 + 10;
constexpr long double EPS = 1e-7;
int n;
int a[N];
long double Check(long double x) {
static long double new_a[N];
long double bestmin = 1e8, curmin = 0;
long double bestmax = -1e8, curmax = 0;
for (int i = 0; i < n; ++i) {
new_a[i] = (long double)a[i] - x;
curmax = max((long double)0, curmax + new_a[i]);
bestmax = max(bestmax, curmax);
curmin = min((long double)0, curmin + new_a[i]);
bestmin = min(bestmin, curmin);
}
return max(abs(bestmin), abs(bestmax));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
long double l = -1e4 - 10, r = 1e4 + 10;
for (int step = 0; step < 500; ++step) {
long double m1 = l + (r - l) / 3.0;
long double m2 = r - (r - l) / 3.0;
if (Check(m1) - Check(m2) > EPS) {
l = m1;
} else {
r = m2;
}
}
printf("%.6lf", (double)Check(l));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 7005;
const double long EPS = 1e-12;
const double long INF = 1e9;
vector<int> ARR, ARR1;
int N;
double long largest1(double long x, vector<int> A) {
double long max_h = -0, max_s = -INF;
for (int i = 0; i < N; i++) {
max_h += A[i] - x;
max_s = max(max_s, max_h);
if (max_h < 0) max_h = 0;
}
return max_s;
}
double long largest2(double long x, vector<int> A) {
double long max_h = -0, max_s = -INF;
for (int i = 0; i < N; i++) {
max_h += A[i] + x;
max_s = max(max_s, max_h);
if (max_h < 0) max_h = 0;
}
return max_s;
}
double long sum(double long x) {
double long s1 = largest1(x, ARR);
double long s2 = largest2(x, ARR1);
s1 = max(s1, -s1);
s2 = max(s2, -s2);
return max(s1, s2);
}
double long ternary(double long lo, double long hi) {
while (hi - lo > EPS) {
double long m1 = lo + (hi - lo) / 3;
double long m2 = hi - (hi - lo) / 3;
if (sum(m1) < sum(m2))
hi = m2;
else
lo = m1;
}
return hi;
}
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) {
int u;
scanf("%d", &u);
ARR.push_back(u);
}
int MIN = 1e4;
int MAX = -1e4;
for (int i = 0; i < N; i++) {
MIN = min(MIN, ARR[i]);
MAX = max(MAX, ARR[i]);
}
for (int i = 0; i < N; i++) ARR1.push_back(-ARR[i]);
double long ans = ternary(MIN, MAX);
ans = sum(ans);
printf("%Lf", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
int n, a[(int)2e5 + 10];
long double WeekNess, mx = -1e18, mn = 1e18, sum;
long double Answer(long double Check) {
mx = 0, mn = 0, sum = 0, WeekNess = 0;
for (int i = 0; i < n; i++) {
mx = max(mx, sum);
mn = min(mn, sum);
sum += a[i] - Check;
WeekNess = max(WeekNess, fabs(sum - mx));
WeekNess = max(WeekNess, fabs(sum - mn));
}
return WeekNess;
}
long double BinarySol(long double l, long double r) {
long double mid1 = (2 * l + r) / 3;
long double mid2 = (2 * r + l) / 3;
int c = 0;
while (c++ <= 200) {
mid1 = (2 * l + r) / 3;
mid2 = (2 * r + l) / 3;
if (Answer(mid1) >= Answer(mid2)) {
l = mid1;
} else {
r = mid2;
}
}
return l;
}
int main() {
cin >> n;
for (int i = 0; i < n && scanf("%d", a + i); i++)
;
cout << setprecision(6) << fixed << Answer(BinarySol(-8e4, 8e4));
}
|
#include <bits/stdc++.h>
using namespace std;
int a[200100], s[200100], v1[200100], v2[200100], sz1, sz2;
int main() {
double mx = 1e20, ans;
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
s[i] = s[i - 1] + a[i];
}
for (int i = 0; i <= n; i++) {
while (sz1 >= 2 &&
1LL * (s[v1[sz1 - 1]] - s[v1[sz1 - 2]]) * (i - v1[sz1 - 2]) >=
1LL * (s[i] - s[v1[sz1 - 2]]) * (v1[sz1 - 1] - v1[sz1 - 2]))
sz1--;
v1[sz1++] = i;
}
for (int i = n; i >= 0; i--) {
while (sz2 >= 2 &&
1LL * (s[v2[sz2 - 1]] - s[v2[sz2 - 2]]) * (i - v2[sz2 - 2]) >=
1LL * (s[i] - s[v2[sz2 - 2]]) * (v2[sz2 - 1] - v2[sz2 - 2]))
sz2--;
v2[sz2++] = i;
}
for (int i = 0, j = 0; i < sz1 - 1 || j < sz2 - 1;) {
double nx1 =
i < sz1 - 1 ? 1.0 * (s[v1[i + 1]] - s[v1[i]]) / (v1[i + 1] - v1[i]) : 0;
double nx2 =
j < sz2 - 1 ? 1.0 * (s[v2[j + 1]] - s[v2[j]]) / (v2[j + 1] - v2[j]) : 0;
double nx;
if (i < sz1 - 1 && j < sz2 - 1 && nx1 < nx2 || j == sz2 - 1) {
i++;
nx = nx1;
} else {
j++;
nx = nx2;
}
double ny1 = s[v1[i]] - nx * v1[i];
double ny2 = s[v2[j]] - nx * v2[j];
if (ny2 - ny1 < mx) {
mx = ny2 - ny1;
ans = nx;
}
}
printf("%.8f\n", mx);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double a[200010];
double b[200010];
double l, r, m, ans1, ans2, res1, res2, ans;
int i, n, x;
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", &x), a[i] = x + 10000;
l = 0;
r = 20000;
ans = 4000000000000LL;
for (int t = 1; t <= 100; t++) {
m = (l + r) / 2;
for (i = 1; i <= n; i++) a[i] -= m;
for (i = 1; i <= n; i++) b[i] = (a[i] * (-1));
ans1 = ans2 = res1 = res2 = 0;
for (i = 1; i <= n; i++) {
ans1 += a[i];
ans2 += b[i];
res1 = max(res1, ans1);
res2 = max(res2, ans2);
if (ans1 < 0) ans1 = 0;
if (ans2 < 0) ans2 = 0;
}
if (res1 < res2) {
ans = min(ans, res2);
r = m;
} else {
ans = min(ans, res1);
l = m;
}
for (i = 1; i <= n; i++) a[i] += m;
}
printf("%.14f\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const double INF = 2e5;
const int LIMIT = 200;
int a[N];
int n;
double getAns(double x) {
double minSum = 0, maxSum = 0, curSum = 0;
double ans = 0;
for (int i = 1; i <= n; ++i) {
curSum += a[i] - x;
ans = max(ans, fabs(curSum - minSum));
ans = max(ans, fabs(curSum - maxSum));
minSum = min(minSum, curSum);
maxSum = max(maxSum, curSum);
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
double l = -INF, r = INF;
double ans = INF * INF;
for (int iter = 0; iter < LIMIT; ++iter) {
double d = (r - l) / 3;
double L = l + d, R = r - d;
double vl = getAns(L), vr = getAns(R);
ans = min(ans, min(vl, vr));
if (vl > vr)
l = L;
else
r = R;
}
cout << setprecision(9) << fixed << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
void rd(T &a) {
a = 0;
char c;
while (c = getchar(), !isdigit(c))
;
do a = a * 10 + (c ^ 48);
while (c = getchar(), isdigit(c));
}
template <class T>
void Min(T &a, T b) {
if (a > b) a = b;
}
template <class T>
void Max(T &a, T b) {
if (a < b) a = b;
}
const int M = 2e5 + 5;
int A[M], n;
inline long double Fabs(long double x) {
if (x < 0) return -x;
return x;
}
long double eps = 1e-15;
inline long double calc(long double x) {
long double Mx = -1e30;
long double sum = 0;
for (int i = 1; i <= n; ++i) {
sum += A[i] - x;
Max(Mx, sum);
if (sum < 0) sum = 0;
}
long double Mn = 1e30;
sum = 0;
for (int i = 1; i <= n; ++i) {
sum += A[i] - x;
Min(Mn, sum);
if (sum > 0) sum = 0;
}
return max(Fabs(Mx), Fabs(Mn));
}
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) scanf("%d", &A[i]);
long double l = -20000, r = 20000;
for (; Fabs(l - r) > eps;) {
long double x1 = l + (long double)(r - l) / 3,
x2 = l + (long double)(r - l) / 3 * 2;
long double t1 = calc(x1), t2 = calc(x2);
if (t1 > t2)
l = x1;
else
r = x2;
}
printf("%.12lf\n", (double)calc(l));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
long double solve(vector<long long>& v, long double x) {
long double mn = 0;
long double mx = 0;
long double ans = 0;
for (int i = 0; i < (int)v.size(); ++i) {
long double cur = 1.0 * v[i] - x * (i + 1);
ans = max(ans, abs(cur - mx));
ans = max(ans, abs(cur - mn));
mx = max(mx, cur);
mn = min(mn, cur);
}
return ans;
}
int main() {
int n;
cin >> n;
vector<long long> v(n);
for (int i = 0; i < (n); ++i) {
scanf("%lld", &v[i]);
if (i) v[i] += v[i - 1];
}
long double l = -3e4, r = +3e4, mid1, mid2;
int iter = 100;
while (iter--) {
mid1 = (r - l) / 3.0;
mid2 = r - mid1;
mid1 += l;
long double v1 = solve(v, mid1);
long double v2 = solve(v, mid2);
if (v1 < v2) {
r = mid2;
} else
l = mid1;
}
mid1 = solve(v, (mid1 + mid2) / 2);
printf("%.15lf\n", (double)mid1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long double eps = 1e-13;
int data[200010];
long double dp[200010][2];
int n;
long double calc(long double x) {
dp[0][0] = dp[0][1] = 0;
long double ans0 = 0, ans1 = 0;
for (int i = 1; i <= n; ++i) {
dp[i][0] = max(dp[i - 1][0] + data[i] - x, data[i] - x);
dp[i][1] = min(dp[i - 1][1] + data[i] - x, data[i] - x);
ans0 = max(ans0, dp[i][0]);
ans1 = min(ans1, dp[i][1]);
}
return max(fabsl(ans0), fabsl(ans1));
}
int main() {
scanf("%d", &n);
long double l = 11000, r = -11000;
for (int i = 1; i <= n; ++i) {
scanf("%d", data + i);
l = min(l, (long double)data[i]);
r = max(r, (long double)data[i]);
}
while (r - l > eps) {
long double mid2 = r * 2 / 3 + l / 3, mid1 = l * 2 / 3 + r / 3;
if (calc(mid1) > calc(mid2))
l = mid1;
else
r = mid2;
}
printf("%f\n", (double)calc((l + r) / 2));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, a[233333];
long double g = asin(1) / 5;
long double f(long double x) {
long double cm = 0, maxm = -2000000001, minm = 2000000001;
for (int i = 1; i <= n; i++) {
long double A = a[i] + x;
if (cm <= 0) cm = 0;
cm += A;
maxm = max(maxm, cm);
}
cm = 0;
for (int i = 1; i <= n; i++) {
long double A = a[i] + x;
if (cm >= 0) cm = 0;
cm += A;
minm = min(minm, cm);
}
cm = 0;
return max(max(maxm, -minm), cm);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", a + i);
long double l = -20000.0, r = 20000.0;
for (int i = 1; i <= 400; i++) {
long double ml = r - l, m1 = l + ml * g, m2 = r - ml * g;
long double x1 = f(m1), x2 = f(m2);
if (x1 > x2)
l = m1;
else
r = m2;
}
long double g = f(l);
double ans = g;
printf("%.6lf\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int N = 1e6;
const long long int inf = 1e14;
const long long int mod = 1e9 + 7;
const double PI = acos(-1);
long long int powm(long long int a, long long int b) {
a = a % mod;
long long int res = 1;
while (b) {
if (b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
long long int n, a[N];
long double f(long double xn) {
set<long double> b = {0};
long double su = 0, ans = 0;
long double ma = 0, mi = 0;
for (int i = 0; i < n; i++) {
su += (long double)a[i] - xn;
ans = max({ans, abs(su - mi), abs(su - ma)});
ma = max(ma, su);
mi = min(mi, su);
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
long long int t = 1;
cout << fixed << std::setprecision(10);
while (t--) {
cin >> n;
long double B = -100000;
long double A = 100000;
for (int i = 0; i < n; i++) {
cin >> a[i];
B = max(B, (long double)a[i]);
A = min(A, (long double)a[i]);
}
for (int i = 0; i < 100; i++) {
long double m1 = (A * 2 + B) / 3.0;
long double m2 = (A + 2 * B) / 3.0;
if (f(m1) > f(m2))
A = m1;
else
B = m2;
}
cout << f(A) << '\n';
}
}
|
#include <bits/stdc++.h>
const long long mod = 1000000007ll;
using namespace std;
int ar[200007];
int n;
double maxe, mine, mxtmp, mntmp;
inline void f(double x) {
mine = 10000;
maxe = -10000;
mxtmp = mntmp = 0;
for (int i = 0; i < n; i++) {
mxtmp += (ar[i] - x);
if (mxtmp < 0) mxtmp = 0;
if (mxtmp > maxe) maxe = mxtmp;
mntmp += (ar[i] - x);
if (mntmp > 0) mntmp = 0;
if (mntmp < mine) mine = mntmp;
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", ar + i);
}
double l = -10000, h = 10000, m;
double minweak = 10000000000;
int lim = 100;
while (lim--) {
m = (l + h) / 2;
f(m);
if (maxe + mine > 0)
l = m;
else
h = m;
}
printf("%.9f\n", maxe);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
c b, e;
};
template <class c>
rge<c> range(c i, c j) {
return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
template <class c>
debug& operator<<(const c&) {
return *this;
}
};
long double check(long double val, vector<long double>& a) {
int n = a.size();
for (int i = 0; i < n; i++) {
a[i] -= val;
}
long double mx = -1e9;
long double curr = -1e9;
long double mn = 1e9;
for (int i = 0; i < n; i++) {
curr = max(curr + a[i], a[i]);
mx = max(curr, mx);
}
curr = 1e9;
for (int i = 0; i < n; i++) {
curr = min(curr + a[i], a[i]);
mn = min(curr, mn);
}
for (int i = 0; i < n; i++) {
a[i] += val;
}
return max(abs(mn), abs(mx));
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<long double> a(n);
for (int i = 0; i < n; i++) {
int chi;
cin >> chi;
a[i] = (long double)chi;
}
long double l = -100005.00, r = 100005.00;
for (int i = 0; i < 350; i++) {
long double m1 = l + (r - l) / 3.0;
long double m2 = r - (r - l) / 3.0;
long double f1 = check(m1, a);
long double f2 = check(m2, a);
if (f1 > f2) {
l = m1;
} else
r = m2;
}
cout << fixed << setprecision(9) << check(r, a) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
double a[1000000];
double f(double x) {
double mn = 0, mx = 0, sm = 0, ret = 0;
for (int i = 0; i < n; i++) {
sm += a[i] - x;
if (sm < 0) {
ret = max(ret, fabs(sm - mx));
} else
ret = max(ret, fabs(sm - mn));
mn = min(mn, sm);
mx = max(mx, sm);
}
return ret;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
double l = -1e5, r = 1e5;
for (int i = 0; i < 300; i++) {
double g = (r - l) / 3 + l;
double h = 2 * (r - l) / 3 + l;
if (f(g) < f(h)) {
r = h;
} else {
l = g;
}
}
cout << fixed << setprecision(10) << f(l) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int a[200001];
int n;
double f(double x) {
double sum = 0, min_sum = 0, max_sum = 0, res = -1e18;
for (int i = 1; i <= n; i++) {
sum += a[i] - x;
res = max(res, fabs(sum - min_sum));
res = max(res, fabs(sum - max_sum));
min_sum = min(min_sum, sum);
max_sum = max(max_sum, sum);
}
return res;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
double l = -1e5, r = 1e5;
for (int i = 1; i <= 100; i++) {
double m1 = l + (r - l) / 3;
double m2 = r - (r - l) / 3;
if (f(m1) < f(m2))
r = m2;
else
l = m1;
}
printf("%.10f", f(l));
}
|
#include <bits/stdc++.h>
using namespace std;
double n, v[1000009 * 2];
double f(double x) {
double ans = 0, sum = 0;
for (int i = 1; i <= n; i++) {
sum += v[i] - x;
ans = max(sum, ans);
if (sum < 0) sum = 0;
}
sum = 0;
double ans1 = 1000000009;
for (int i = 1; i <= n; i++) {
sum += v[i] - x;
ans1 = min(sum, ans1);
if (sum > 0) sum = 0;
}
return max(abs(ans1), ans);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> v[i];
double l = -10000, r = 10000;
for (int i = 1; i <= 100; i++) {
double m1 = l + (r - l) / 3;
double m2 = r - (r - l) / 3;
if (f(m1) < f(m2))
r = m2;
else
l = m1;
}
printf("%.6lf", f(l));
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int a[N], n;
double poorness(double x) {
double csp = 0, bs = 0, csn = 0;
for (int i = 0; i < n; i++) {
csp += (double)a[i] - x;
csn += (double)a[i] - x;
if (csp < 0) csp = 0;
if (csn > 0) csn = 0;
bs = max(bs, csp);
bs = max(bs, -csn);
}
return bs;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
double l = -10000.0, r = 10000.0, ans = 1e10;
int t = 400;
while (t--) {
double m1 = l + (r - l) / 3.0, m2 = r - (r - l) / 3.0;
double fm1 = poorness(m1), fm2 = poorness(m2);
if (fm1 > fm2)
l = m1;
else
r = m2;
ans = min(ans, fm1);
ans = min(ans, fm2);
}
cout << fixed << setprecision(10) << ans;
}
|
#include <bits/stdc++.h>
const double f1 = (double)1 / 3, f2 = (double)2 / 3;
const double INF = 1e9;
int a[200005], n;
inline double max(double a, double b) { return a > b ? a : b; }
double poor(double x) {
double m = 0, M = 0, sum = INF, suM = -INF;
double ret;
for (int i = 0; i < n; i++) {
double num = a[i] - x;
if (sum > 0)
sum = num;
else
sum += num;
if (suM < 0)
suM = num;
else
suM += num;
ret = max(ret, max(fabs(sum), fabs(suM)));
}
return ret;
}
double solve() {
double r[2] = {-1e4 - 1, 1e4 + 1};
for (int t = 0; t < 100 && r[0] < r[1]; t++) {
double v[2], diff = r[1] - r[0];
printf("");
v[0] = poor(r[0] + f1 * diff);
v[1] = poor(r[0] + f2 * diff);
if (v[0] < v[1])
r[1] = r[0] + f2 * diff;
else
r[0] = r[0] + f1 * diff;
}
return poor(r[0]);
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("%f\n", solve());
}
|
#include <bits/stdc++.h>
double f_abs(double x) { return x < 0 ? -x : x; }
double f_max(double a, double b) { return a > b ? a : b; }
double f_min(double a, double b) { return a < b ? a : b; }
const double eps = 1e-12;
const int N = 200003;
int n;
double a[N], b[N];
void GetData() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%lf", &a[i]);
}
double Calc(double x) {
int i;
double dp, cur, res;
for (i = 0; i < n; ++i) b[i] = a[i] - x;
dp = cur = b[0];
for (i = 1; i < n; ++i) {
if (cur + b[i] < b[i])
cur = b[i];
else
cur += b[i];
dp = f_max(dp, cur);
}
res = f_abs(dp);
dp = cur = b[0];
for (i = 1; i < n; ++i) {
if (cur + b[i] > b[i])
cur = b[i];
else
cur += b[i];
dp = f_min(dp, cur);
}
res = f_max(res, f_abs(dp));
return res;
}
void Solve() {
int cnt = 100;
double lo, hi, midl, midr;
lo = -1e4;
hi = 1e4;
while (fabs(hi - lo) > eps && cnt--) {
midl = (hi + lo * 2) / 3;
midr = (hi * 2 + lo) / 3;
if (Calc(midl) > Calc(midr))
lo = midl;
else
hi = midr;
}
printf("%.10lf\n", Calc(lo));
}
int main() {
GetData();
Solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-11;
const int maxn = 200000 + 100;
int a[maxn];
double l[maxn];
int n;
double cal(double x) {
double sum = 0;
for (int i = 0; i < n; i++) l[i] = a[i] * 1.0 - x;
double cur = 0;
for (int i = 0; i < n; i++) {
cur = cur + l[i];
if (cur < 0) cur = 0;
sum = max(sum, cur);
l[i] = -l[i];
}
cur = 0;
for (int i = 0; i < n; i++) {
cur = cur + l[i];
if (cur < 0) cur = 0;
sum = max(sum, cur);
}
return sum;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
double l = -1e4, r = 1e4, m1, m2, v1, v2;
int t = 100;
while (t--) {
m1 = l + (r - l) / 3;
m2 = r - (r - l) / 3;
v1 = cal(m1);
v2 = cal(m2);
if (v1 > v2)
l = m1;
else
r = m2;
}
printf("%.15f\n", cal(l));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool getmin(T *a, const T &b) {
if (b < *a) {
*a = b;
return true;
}
return false;
}
template <class T>
inline bool getmax(T *a, const T &b) {
if (b > *a) {
*a = b;
return true;
}
return false;
}
template <class T>
inline void read(T *a) {
char c;
while (isspace(c = getchar())) {
}
bool flag = 0;
if (c == '-')
flag = 1, *a = 0;
else
*a = c - 48;
while (isdigit(c = getchar())) *a = *a * 10 + c - 48;
if (flag) *a = -*a;
}
const int mo = 1000000007;
template <class T>
T pow(T a, T b, int c = mo) {
T res = 1;
for (T i = 1; i <= b; i <<= 1, a = 1LL * a * a % c)
if (b & i) res = 1LL * res * a % c;
return res;
}
const int N = 210000;
double a[N];
int n;
double check(double x) {
double tmp = 0, res = 0;
for (int i = (1); i <= (n); ++i) {
tmp = max(a[i] - x, tmp + a[i] - x);
getmax(&res, tmp);
}
tmp = 0;
for (int i = (1); i <= (n); ++i) {
tmp = min(a[i] - x, tmp + a[i] - x);
getmax(&res, -tmp);
}
return res;
}
int main() {
cin >> n;
for (int i = (1); i <= (n); ++i) read(a + i);
double l = -2e9, r = 2e9, ans = 2e9;
for (int tt = (1); tt <= (200); ++tt) {
double m1 = (l + r) / 2;
double m2 = (m1 + r) / 2;
double fl = check(l), fr = check(r), f1 = check(m1), f2 = check(m2);
getmin(&ans, fl);
getmin(&ans, f1);
getmin(&ans, f2);
getmin(&ans, fr);
if (f1 < f2)
r = m2;
else
l = m1;
}
printf("%.10lf\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
double a[N], b[N];
int n;
double check(double x) {
int i;
double ans, ans1 = 0, ans2 = 0, sum1 = 0, sum2 = 0;
for (i = 1; i <= n; i++) {
b[i] = a[i] - x;
}
for (i = 1; i <= n; i++) {
sum1 += b[i];
if (sum1 < 0) sum1 = 0;
ans1 = max(sum1, ans1);
b[i] = -b[i];
}
for (i = 1; i <= n; i++) {
sum2 += b[i];
if (sum2 < 0) sum2 = 0;
ans2 = max(sum2, ans2);
}
ans = max(ans1, ans2);
return ans;
}
int main() {
int i;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%lf", &a[i]);
}
double l = -10000, r = 10000, mid, midl, midr;
for (i = 1; i <= 100; i++) {
mid = (r - l) / 3.0;
midl = l + mid;
midr = r - mid;
if (check(midl) > check(midr)) {
l = midl;
} else
r = midr;
}
printf("%.8f\n", check(l));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int p, q, r, n, a[200005];
double eps = 5e-7;
double mid(double a, double b) {
if (a > b) swap(a, b);
return a + (b - a) * 0.618;
}
double fun(double x) {
double MAX, MIN, sum;
int i;
MAX = MIN = sum = 0;
for (i = 0; i < n; i++) {
sum += a[i] - x;
MAX = max(MAX, sum);
MIN = min(MIN, sum);
}
return MAX - MIN;
}
double dis(double a, double b) { return fabs(a - b); }
double find() {
double m1, m2, l, r;
double cl, cr, cm1, cm2;
r = 100000;
l = -100000;
cl = fun(l);
cr = fun(r);
m2 = mid(r, l);
cm2 = fun(m2);
m1 = mid(l, m2);
cm1 = fun(m1);
while (fabs(l - r) + fabs(cl - cr) > eps) {
if (cm1 < cm2) {
r = m2;
cr = cm2;
m2 = m1;
cm2 = cm1;
m1 = mid(l, m2);
cm1 = fun(m1);
} else {
l = m1;
cl = cm1;
m1 = m2;
cm1 = cm2;
m2 = mid(r, l);
cm2 = fun(m2);
}
}
return (cm1 + cm2) / 2;
}
int main() {
int t, i;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
printf("%.10f\n", find());
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int n, a[200005];
long double eps = 1e-11, b[200005], s[200005];
long double f(long double first) {
long double mn = 0, ans = 0;
for (int i = 1; i <= n; i++) {
b[i] = -first + a[i];
s[i] = s[i - 1] + b[i];
ans = max(ans, s[i] - mn);
mn = min(mn, s[i]);
}
mn = 0;
for (int i = 1; i <= n; i++) {
ans = max(ans, mn - s[i]);
mn = max(mn, s[i]);
}
return ans;
}
int32_t main() {
ios_base ::sync_with_stdio(0);
cin.tie();
cout.tie();
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
long double l = -1e9, r = 1e9;
for (int i = 1; i <= 200; i++) {
long double mid1 = (l * 2 + r) / 3, mid2 = (l + r * 2) / 3;
if (f(mid1) > f(mid2))
l = mid1;
else if (f(mid1) < f(mid2))
r = mid2;
else
l = mid1, r = mid2;
}
cout << fixed << setprecision(7) << f(l);
}
|
#include <bits/stdc++.h>
using namespace std;
const int iinf = 0x7fffffff;
const long long linf = ~(1LL << 63);
const int maxn = 2e5 + 5;
int n;
double a[maxn];
double b[maxn];
double max_con_seq(double* seq, int len) {
double ans = -linf;
double now = 0;
for (int i = 0; i < len; i++) {
now += seq[i];
if (ans < now) ans = now;
if (now < 0) now = 0;
}
return ans;
}
double wrapper(double x) {
double ans = -linf;
for (int i = 0; i < n; i++) b[i] = a[i] - x;
ans = max_con_seq(b, n);
for (int i = 0; i < n; i++) b[i] = x - a[i];
ans = max(ans, max_con_seq(b, n));
return ans;
}
void read() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
}
void solve() {
double l = -20050, r = 20050;
double ans = wrapper((l + r) / 2);
double last_ans = -linf;
while (fabs(l - r) > 1e-11) {
double lm = (r + 2 * l) / 3;
double rm = (2 * r + l) / 3;
double lv = wrapper(lm), rv = wrapper(rm);
last_ans = ans;
if (lv <= rv) {
r = rm;
ans = lv;
} else {
l = lm;
ans = rv;
}
}
cout << fixed << setprecision(10) << ans << endl;
}
int main() {
ios::sync_with_stdio(0);
read();
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 3e-12;
const int N = (int)2e6 + 10;
double a[N], b[N];
int n;
double maxSum(double A[]) {
double ans = 0, tmp = 0;
for (int i = 0; i < n; i++) {
tmp += A[i];
if (tmp < 0) tmp = 0;
ans = max(ans, tmp);
}
return ans;
}
double calc(double x) {
for (int i = 0; i < n; i++) b[i] = a[i] - x;
double ans1 = maxSum(b);
for (int i = 0; i < n; i++) b[i] = -b[i];
double ans2 = maxSum(b);
return max(ans1, ans2);
}
double search() {
double left = -1e4, right = 1e4;
double midl, midr;
while (left + eps < right) {
midl = (2 * left + right) / 3.0;
midr = (left + 2 * right) / 3.0;
double w1 = calc(midl);
double w2 = calc(midr);
if (w1 < w2)
right = midr;
else
left = midl;
}
return calc(right);
}
int main() {
while (~scanf("%d", &n)) {
for (int i = 0; i < n; i++) {
scanf("%lf", &a[i]);
}
printf("%.15f\n", search());
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
long double a[N];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
long double ans;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
long double st = -10010, en = 10010;
for (int t = 0; t < 70; t++) {
long double m = (st + en) / 2;
long double minn = 0, maxx = 0, sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i] + m;
minn = min(minn, sum);
maxx = max(maxx, sum);
}
vector<int> v[2];
if (minn == 0) v[0].push_back(-1);
if (maxx == 0) v[1].push_back(-1);
ans = maxx - minn;
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i] + m;
if (sum == minn) v[0].push_back(i);
if (sum == maxx) v[1].push_back(i);
}
if (v[0].back() <= v[1][0]) en = m;
if (v[1].back() <= v[0][0]) st = m;
}
cout << fixed << setprecision(10) << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
const long long INF = 1LL << 62;
const long double pi = acos(-1);
const int mod = 1e9 + 7;
struct ii {
int x, y;
ii(int x = 0, int y = 0) : x(x), y(y) {}
bool operator==(const ii &a) const { return x == a.x && y == a.y; }
bool operator<(const ii &a) const {
if (x != a.x) return x < a.x;
return y < a.y;
}
ii friend operator+(ii a, ii b) { return ii(a.x + b.x, a.y + b.y); }
ii friend operator-(ii a, ii b) { return ii(a.x - b.x, a.y - b.y); }
};
template <class T>
void sc(T &x) {
char c;
x = 0;
int t = 1;
while (c = getchar(), c < 48)
if (c == '-') t = -1;
do x = x * 10 + (c ^ 48);
while (c = getchar(), c > 47);
x = x * t;
}
template <class T>
void nt(T x) {
if (!x) return;
nt(x / 10);
putchar(x % 10 + '0');
}
template <class T>
void pt(T x) {
if (x < 0) putchar('-'), x = -x;
if (!x)
putchar('0');
else
nt(x);
}
template <class T>
void pts(T x) {
pt(x);
putchar(' ');
}
template <class T>
void ptn(T x) {
pt(x);
putchar('\n');
}
template <class T>
void pp(T x, int y) {
static char ch[] = {" \n"};
pt(x);
putchar(ch[y]);
}
template <class T>
void Max(T &x, T y) {
if (x < y) x = y;
}
template <class T>
void Min(T &x, T y) {
if (x > y) x = y;
}
const int maxn = 200005;
int n;
int d[maxn];
double solve(double x) {
double t = 0, mx = 0, mn = 0;
for (int i = 0, i_ = (n)-1; i <= i_; ++i) {
t += d[i] - x;
Max(mx, t);
Min(mn, t);
}
return fabs(mx - mn);
}
int main() {
sc(n);
for (int i = 0, i_ = (n)-1; i <= i_; ++i) sc(d[i]);
double l = -10000, r = 10000;
int cas = 100;
while (cas--) {
double k = (r - l) / 3;
double l1 = l + k;
double l2 = l + k + k;
double ans1 = solve(l1);
double ans2 = solve(l2);
if (ans1 < ans2)
r = l2;
else
l = l1;
}
double res = solve(l);
printf("%.12f\n", res);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
const int inf = maxn;
int n;
int a[maxn];
int main() {
cin >> n;
int maxa = -inf, mina = inf;
for (int i = (1); i <= (n); i++) {
scanf("%d", &a[i]);
maxa = ((maxa) > (a[i]) ? (maxa) : (a[i]));
mina = ((mina) > (a[i]) ? (a[i]) : (mina));
}
double l = mina, r = maxa;
double mid1 = 0, mid2 = 10, ans1, ans2;
double maxdp, mindp, maxp, minp;
for (int ii = (0); ii <= (200); ii++) {
mid1 = l + (r - l) / 3;
mid2 = r - (r - l) / 3;
maxdp = mindp = maxp = minp = 0;
for (int i = (1); i <= (n); i++) {
maxdp = (maxdp + a[i] - mid1) < 0 ? 0 : maxdp + a[i] - mid1;
maxp = ((maxp) > (maxdp) ? (maxp) : (maxdp));
mindp = (mindp + a[i] - mid1) > 0 ? 0 : mindp + a[i] - mid1;
minp = ((minp) > (mindp) ? (mindp) : (minp));
}
ans1 = max(fabs(maxp), fabs(minp));
maxdp = mindp = maxp = minp = 0;
for (int i = (1); i <= (n); i++) {
maxdp = (maxdp + a[i] - mid2) < 0 ? 0 : maxdp + a[i] - mid2;
maxp = ((maxp) > (maxdp) ? (maxp) : (maxdp));
mindp = (mindp + a[i] - mid2) > 0 ? 0 : mindp + a[i] - mid2;
minp = ((minp) > (mindp) ? (mindp) : (minp));
}
ans2 = max(fabs(maxp), fabs(minp));
if (ans1 > ans2)
l = mid1;
else
r = mid2;
}
printf("%.10lf\n", ans2);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int n;
double a[maxn];
double cal(double x) {
double Maxn = 0, Minn = 0;
double cur = 0;
for (int i = 0; i < n; i++) {
cur += a[i] - x;
Maxn = max(Maxn, cur);
Minn = min(Minn, cur);
}
return Maxn - Minn;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%lf", &a[i]);
double L = -1e4, R = 1e4;
for (int i = 0; i < 200; i++) {
double m1 = (L + R) / 2;
double m2 = (m1 + R) / 2;
if (cal(m1) < cal(m2))
R = m2;
else
L = m1;
}
printf("%.10f\n", cal(L));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
double value[MAXN];
int n;
double fun(double x) {
double mn = 0, mx = 0, sum = 0;
for (int i = 0; i < n; i++) {
sum += (value[i] - x);
mn = min(sum, mn);
mx = max(sum, mx);
}
return (mx - mn);
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> value[i];
}
double l = -10000.0, r = 10000.0;
for (int i = 0; i < 100; i++) {
double m1 = l + (r - l) / 3.0;
double m2 = r - (r - l) / 3.0;
if (fun(m1) < fun(m2))
r = m2;
else
l = m1;
}
cout << fixed << setprecision(6) << fun(l) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int i, j, k, l, n, a[200010];
double t, q, p, o, s[200010];
double ab(double x) {
if (x < 0) x = -x;
return x;
}
double cal(double p) {
int i;
s[0] = 0;
for (i = 1; i <= n; i++) s[i] = s[i - 1] + a[i] - p;
double mi = 0, ma = 0, ansx = -10000000, ansn = 1000000;
for (i = 1; i <= n; i++) {
if (s[i] < mi) mi = s[i];
if (s[i] > ma) ma = s[i];
if (s[i] - mi > ansx) ansx = s[i] - mi;
if (s[i] - ma < ansn) ansn = s[i] - ma;
}
if (ansx < 0) ansx = -ansx;
if (ansn < 0) ansn = -ansn;
if (ansn > ansx) ansx = ansn;
return (ansx);
}
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
double l = -10000, r = 10000, tl, tr, tm, tm2, m, m2;
tl = cal(l);
tr = cal(r);
while (1) {
m = (r + l) / 2.0;
tm = cal(m);
if (ab(tr - tl) < 0.000001) break;
m2 = (r + m) / 2.0;
tm2 = cal(m2);
if (tm > tm2) {
l = m;
tl = tm;
} else {
r = m2;
tr = tm2;
}
}
printf("%.9f\n", tm);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long MulModL(long long B, long long P, long long M) {
long long R = 0;
while (P > 0) {
if ((P & 1ll) == 1) {
R = (R + B);
if (R >= M) R -= M;
}
P >>= 1ll;
B = (B + B);
if (B >= M) B -= M;
}
return R;
}
long long MulModD(long long B, long long P, long long M) {
long long I = ((long double)B * (long double)P / (long double)M);
long long R = B * P - M * I;
R = (R % M + M) % M;
return R;
}
long long BigMod(long long B, long long P, long long M) {
long long R = 1;
while (P > 0) {
if (P % 2 == 1) {
R = (R * B) % M;
}
P /= 2;
B = (B * B) % M;
}
return R;
}
template <class T1>
void deb(T1 e1) {
cout << e1 << endl;
}
template <class T1, class T2>
void deb(T1 e1, T2 e2) {
cout << e1 << " " << e2 << endl;
}
template <class T1, class T2, class T3>
void deb(T1 e1, T2 e2, T3 e3) {
cout << e1 << " " << e2 << " " << e3 << endl;
}
template <class T1, class T2, class T3, class T4>
void deb(T1 e1, T2 e2, T3 e3, T4 e4) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl;
}
template <class T1, class T2, class T3, class T4, class T5>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl;
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6
<< endl;
}
const int MAX = 200009;
double val[MAX];
int n;
double getRet(double m) {
double sum = 0;
double res = 0;
for (int i = 1; i <= n; i++) {
sum += (val[i] - m);
res = max(res, fabs(val[i] - m));
}
res = max(res, fabs(sum));
sum = 0;
double s = 1.0;
for (int i = 1; i <= n; i++) {
sum += (val[i] - m) * s;
if (sum < 0.0) {
sum = 0;
}
res = max(res, sum);
}
s = -1.0;
sum = 0;
for (int i = 1; i <= n; i++) {
sum += (val[i] - m) * s;
if (sum < 0.0) {
sum = 0;
}
res = max(res, sum);
}
return res;
}
int main() {
scanf("%d", &n);
int v;
double sum = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &v);
val[i] = v;
}
double res = 0;
int cnt = 500;
double lo = -2.0e10, hi = 2.0e10, m1, m2;
while (cnt--) {
m1 = (2.0 * lo + hi) / 3.0;
m2 = (lo + hi * 2.0) / 3.0;
double r1 = getRet(m1), r2 = getRet(m2);
if (r1 < r2) {
res = r1;
hi = m2;
} else {
res = r2;
lo = m1;
}
}
printf("%.12lf", res);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
double a[200010];
double b[200010];
int n;
double Cal(double x) {
double Max = 0, Min = 0;
double best = 0.0;
for (int i = 1; i <= n; ++i) {
Max = max(0.0, Max) + (a[i] - x);
best = max(best, fabs(Max));
Min = min(0.0, Min) + (a[i] - x);
best = max(best, fabs(Min));
}
return best;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lf", &a[i]);
double left = -1000000, right = 1000000;
double mid, midmid;
for (int i = 0; i < 300; i++) {
mid = (left + right) / 2;
midmid = (mid + right) / 2;
if (Cal(mid) < Cal(midmid))
right = midmid;
else
left = mid;
}
printf("%lf\n", Cal(right));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
const double e = 1e-11;
double a[N];
int n;
double miin, maax;
double Min, Max;
void Do(double t) {
Min = 1e9;
Max = -1e9;
double sum = 0;
double in = 0, ax = 0;
for (int i = 0; i < n; ++i) {
sum += a[i] + t;
in = min(in, sum);
ax = max(ax, sum);
Min = min(Min, sum - ax);
Max = max(Max, sum - in);
}
if (!Min) Min = miin + t;
if (!Max) Max = maax + t;
}
void binup() {
double l = 0, r = 10000;
while (r - l > e) {
double m = (l + r) / 2;
Do(m);
if (-Min < Max)
r = m;
else
l = m;
}
Do(l);
}
void bindown() {
double l = -10000, r = 0;
while (r - l > e) {
double m = (l + r) / 2;
Do(m);
if (-Min > Max)
l = m;
else
r = m;
}
Do(l);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> n;
miin = 1e9;
maax = -1e9;
for (int i = 0; i < n; ++i) {
cin >> a[i];
miin = min(miin, a[i]);
maax = max(maax, a[i]);
}
Do(0);
if (-Min < Max)
bindown();
else
binup();
printf("%0.12f", (Max - Min) / 2);
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n;
int a[N];
double maxSuff[N];
double minSuff[N];
double mx[N], mn[N];
double f(double x) {
double tmp;
for (int i = 1; i <= n; i++) {
tmp = a[i] - x;
mx[i] = mn[i] = maxSuff[i] = minSuff[i] = 0;
maxSuff[i] = max(0.0, maxSuff[i - 1] + tmp);
minSuff[i] = min(0.0, minSuff[i - 1] + tmp);
mx[i] = max(mx[i - 1], maxSuff[i - 1] + tmp);
mn[i] = min(mn[i - 1], minSuff[i - 1] + tmp);
}
return max(abs(mx[n]), abs(mn[n]));
}
double tsearch(double l, double r) {
double a, b;
for (int i = 0; i < 500; i++) {
a = l + (r - l) / 3;
b = r - (r - l) / 3;
if (f(a) < f(b)) {
r = b;
} else {
l = a;
}
}
return (r + l) / 2;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
printf("%.8lf\n", f(tsearch(-2e9 - 9, 2e9 + 7)));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long double findWeakness(int arr[], int n, long double x) {
long double minimum = 0, maximum = 0, ans = 0, sum = 0;
for (int i = 0; i < n; i++) {
sum += (arr[i] - x);
long double currBest;
if (sum < 0)
currBest = sum - maximum;
else
currBest = sum - minimum;
ans = max(ans, abs(currBest));
if (sum < minimum) minimum = sum;
if (sum > maximum) maximum = sum;
}
return ans;
}
long double weaknessAndPoorness(int arr[], int n) {
long double start = INT_MIN, end = INT_MAX, precision = 1e-9, ans = 1e18;
for (int i = 0; i < 300; i++) {
long double midLeft = start + (end - start) / 3,
midRight = end - (end - start) / 3;
long double costLeft = findWeakness(arr, n, midLeft),
costRight = findWeakness(arr, n, midRight);
ans = min(ans, min(costLeft, costRight));
if (costLeft < costRight)
end = midRight;
else
start = midLeft;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
long double ans = weaknessAndPoorness(arr, n);
printf("%.12Lf", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long double findWeakness(int arr[], int n, long double x) {
long double minimum = 0, maximum = 0, ans = 0, sum = 0;
for (int i = 0; i < n; i++) {
sum += (arr[i] - x);
long double currBest;
if (sum < 0)
currBest = sum - maximum;
else
currBest = sum - minimum;
ans = max(ans, abs(currBest));
if (sum < minimum) minimum = sum;
if (sum > maximum) maximum = sum;
}
return ans;
}
long double weaknessAndPoorness(int arr[], int n) {
long double start = INT_MIN, end = INT_MAX, precision = 1e-9, ans = 1e18;
for (int i = 0; i < 300; i++) {
long double midLeft = start + (end - start) / 3,
midRight = end - (end - start) / 3;
long double costLeft = findWeakness(arr, n, midLeft),
costRight = findWeakness(arr, n, midRight);
ans = min(ans, min(costLeft, costRight));
if (costLeft < costRight)
end = midRight;
else
start = midLeft;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
long double ans = weaknessAndPoorness(arr, n);
printf("%.12Lf", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
int A[400001];
double P[400001];
double f(double x) {
P[0] = 0;
double max_seg = 0, min_seg = 0, max_p = 0, min_p = 0;
for (int i = 1; i <= n; i++) {
P[i] = P[i - 1] + A[i] - x;
max_p = max(max_p, P[i]);
min_p = min(min_p, P[i]);
max_seg = max(max_seg, P[i] - min_p);
min_seg = min(min_seg, P[i] - max_p);
}
return max(max_seg, -min_seg);
}
double ternary_search(double l, double r, double eps) {
double m1, m2, val1, val2;
while (r - l >= eps) {
m1 = l + (r - l) / 3;
m2 = r - (r - l) / 3;
val1 = f(m1);
val2 = f(m2);
if (val1 == val2) {
l = m1;
r = m2;
} else if (val1 < val2) {
r = m2;
} else {
l = m1;
}
}
return l;
}
double ternary_search_max(double l, double r, double eps) {
double m1, m2, val1, val2;
while (r - l >= eps) {
m1 = l + (r - l) / 3;
m2 = r - (r - l) / 3;
val1 = f(m1);
val2 = f(m2);
if (val1 == val2) {
l = m1;
r = m2;
} else if (val1 < val2) {
l = m1;
} else {
r = m2;
}
}
return l;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
A[0] = 0;
int maxi = 0;
for (int i = 1; i <= n; i++) {
cin >> A[i];
if (A[i] > maxi) maxi = A[i];
}
cout.precision(13);
cout << fixed << f(ternary_search(-10000, 10000, 1e-13)) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200100;
const long double inf = 1e15;
const long double eps = 1e-15;
int n, a[maxn];
long double dabs(long double d) {
if (d < 0)
return d * -1;
else
return d;
}
long double go(long double k) {
long double mi = 0, ma = 0, cur = 0;
long double ans = 0;
for (int i = 0; i < n; i++) {
cur += a[i] - k;
ans = max(ans, max(dabs(cur - mi), dabs(cur - ma)));
mi = min(mi, cur);
ma = max(ma, cur);
}
return ans;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
long double lo = -10005, hi = 10005;
while (hi - lo > eps) {
long double m1 = lo + (hi - lo) / 3, m2 = lo + 2 * (hi - lo) / 3;
if (go(m1) < go(m2))
hi = m2;
else
lo = m1;
}
cout << fixed << setprecision(10) << go(lo) << '\n';
}
|
#include <bits/stdc++.h>
#pragma GCC target("avx,avx2,fma")
using namespace std;
template <typename T>
int len(const T& a) {
return a.size();
}
using ll = long long;
constexpr int N = 2e5 + 10;
constexpr long double EPS = 1e-7;
int n;
int a[N];
long double Check(long double x) {
static long double new_a[N];
long double bestmin = 1e8, curmin = 0;
long double bestmax = -1e8, curmax = 0;
for (int i = 0; i < n; ++i) {
new_a[i] = (long double)a[i] - x;
curmax = max((long double)0, curmax + new_a[i]);
bestmax = max(bestmax, curmax);
curmin = min((long double)0, curmin + new_a[i]);
bestmin = min(bestmin, curmin);
}
return max(abs(bestmin), abs(bestmax));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
long double l = -1e4 - 10, r = 1e4 + 10;
for (int step = 0; step < 300; ++step) {
long double m1 = l + (r - l) / 3.0;
long double m2 = r - (r - l) / 3.0;
if (Check(m1) > Check(m2)) {
l = m1;
} else {
r = m2;
}
}
printf("%.6lf", (double)Check(l));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 3e-12;
int n;
double a[200005], b[200005];
double maxsum() {
double ans = 0, tmp = 0;
for (int i = 1; i <= n; i++) {
tmp += b[i];
if (tmp < 0) tmp = 0;
ans = max(tmp, ans);
}
return ans;
}
double cal(double x) {
for (int i = 1; i <= n; i++) b[i] = a[i] - x;
double ans1 = maxsum();
for (int i = 1; i <= n; i++) b[i] = -b[i];
double ans2 = maxsum();
return max(ans1, ans2);
}
int main() {
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; i++) scanf("%lf", &a[i]);
double l = -10005, r = 10005;
while (r - l > eps) {
double m1 = l + (r - l) / 3.0;
double m2 = r - (r - l) / 3.0;
if (cal(m1) < cal(m2))
r = m2;
else
l = m1;
}
printf("%.15f\n", cal(l));
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, a[200005], sp[200005];
double val(double x) {
double mx = sp[0], mn = sp[0];
for (int i = 1; i <= n; i++) {
mx = (((mx) > (sp[i] - i * x)) ? (mx) : (sp[i] - i * x));
mn = (((mn) < (sp[i] - i * x)) ? (mn) : (sp[i] - i * x));
}
return mx - mn;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
sp[i] = sp[i - 1] + a[i];
}
double l = -10002, r = 10002, mid1, mid2;
for (int iter = 1; iter <= 200; iter++) {
double x = (r - l) / 3;
mid1 = l + x;
mid2 = r - x;
double xx = val(mid1), yy = val(mid2);
if (fabs(xx - yy) < 1e-9) break;
if (xx < yy)
r = mid2;
else
l = mid1;
}
printf("%.10f\n", val((l + r) / 2));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-11;
double calc(vector<double> &v, double nm) {
int n = int((v).size());
double mx = 0;
double cur = 0;
for (int i = 0; i < n; i++) {
if (cur < 0) {
cur = 0;
}
cur += (v[i] - nm);
mx = max(mx, cur);
}
cur = 0;
for (int i = 0; i < n; i++) {
if (cur > 0) {
cur = 0;
}
cur += (v[i] - nm);
mx = max(mx, abs(cur));
}
return mx;
}
int main() {
std::ios_base::sync_with_stdio(0);
int n;
cin >> n;
vector<double> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
double l = -1000000000, r = 1000000000;
for (int i = 0; i < 350; i++) {
double m1 = l + (r - l) / 3, m2 = r - (r - l) / 3;
if (calc(v, m1) >= calc(v, m2)) {
l = m1;
} else
r = m2;
}
cout << fixed;
cout << setprecision(14);
cout << min(calc(v, l), calc(v, r)) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
int n;
long double arr[N];
long double f(long double x) {
long double minS = 0, maxS = 0, curS = 0, ans = 0;
for (int i = 0; i < n; i++) {
curS += arr[i] - x;
ans = max(ans, abs(curS - minS));
ans = max(ans, abs(curS - maxS));
minS = min(minS, curS);
maxS = max(maxS, curS);
}
return ans;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
long double l = -3e9, r = 3e9;
for (int i = 0; i < 200; i++) {
long double mid1 = (l * 2 + r) / 3, mid2 = (l + r * 2) / 3;
long double f1 = f(mid1), f2 = f(mid2);
if (f1 > f2)
l = mid1;
else
r = mid2;
}
cout.precision(20);
cout << fixed << f((l + r) / 2) << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 3e-12;
const int N = (int)2e6 + 10;
double a[N], b[N];
int n;
double maxSum(double a[]) {
double ans = 0, tmp = 0;
for (int i = 0; i < n; i++) {
tmp += a[i];
if (tmp < 0) tmp = 0;
ans = max(ans, tmp);
}
return ans;
}
double calc(double x) {
for (int i = 0; i < n; i++) b[i] = a[i] - x;
double ans1 = maxSum(b);
for (int i = 0; i < n; i++) b[i] *= -1;
double ans2 = maxSum(b);
return max(ans1, ans2);
}
double Search() {
double left = -1e4, right = 1e4;
double midl, midr;
while (right - left > eps) {
midl = (2 * left + right) / 3.0;
midr = (left + 2 * right) / 3.0;
double w1 = calc(midl);
double w2 = calc(midr);
if (w1 < w2)
right = midr;
else
left = midl;
}
return calc(right);
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%lf", &a[i]);
printf("%.15f\n", Search());
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n;
vector<double> v;
double func(double x) {
double sum = (v[0] - x);
int now;
if ((v[0] - x) < 0)
now = -1;
else
now = 1;
vector<double> a1;
vector<double> a2;
for (int i = 1; i < n; i++) {
if (v[i] - x <= 0) {
if (now == -1)
sum += (v[i] - x);
else {
now = -1;
a1.push_back(sum);
a2.push_back(-sum);
sum = v[i] - x;
}
} else {
if (now == 1)
sum += (v[i] - x);
else {
a1.push_back(sum);
a2.push_back(-sum);
now = 1;
sum = v[i] - x;
}
}
}
a1.push_back(sum);
a2.push_back(-sum);
double ans = 0;
sum = 0;
for (int i = 0; i < a1.size(); i++) {
sum += a1[i];
ans = max(sum, ans);
if (sum < 0) sum = 0;
}
sum = 0;
for (int i = 0; i < a2.size(); i++) {
sum += a2[i];
ans = max(ans, sum);
if (sum < 0) sum = 0;
}
return ans;
}
void solve() {
cin >> n;
v = vector<double>(n);
for (int i = 0; i < n; i++) cin >> v[i];
double lo = -1e4 - 10;
double hi = 1e4 + 10;
int cnt = 0;
while (true) {
double m1 = lo + (hi - lo) / 3;
double m2 = hi - (hi - lo) / 3;
double f1 = func(m1);
double f2 = func(m2);
if (f1 > f2) {
lo = m1;
} else
hi = m2;
cnt++;
if (cnt == 100) break;
}
printf("%0.9f\n", func(lo));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tests = 1;
while (tests--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
void FastIO() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
int n;
vector<int> a;
double foo(double val) {
double ret1 = -INT_MAX, ret2 = -INT_MAX, temp1 = 0, temp2 = 0;
for (int i = 0; i < (int)n; i++) {
temp1 += a[i] - val;
temp1 *= (temp1 >= 0);
ret1 = max(ret1, temp1);
temp2 += a[i] - val;
temp2 *= (temp2 <= 0);
ret2 = max(ret2, fabs(temp2));
}
return max(ret1, ret2);
}
int main() {
FastIO();
cin >> n;
a.assign(n, 0);
for (int i = 0; i < (int)n; i++) {
cin >> a[i];
}
double l = -20000, r = 20000, mid1, mid2, cnt = 400, ans = 0;
while (cnt--) {
mid1 = l + (r - l) / 3;
mid2 = r - ((r - l) / 3);
double val1 = foo(mid1);
double val2 = foo(mid2);
if (mid1 == mid2) {
ans = val1;
break;
} else if (val1 < val2) {
r = mid2;
} else {
l = mid1;
}
}
cout << setprecision(10) << fixed << ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double EXP = 1e-11;
const int maxn = 2e5 + 10;
int n;
double a[maxn];
double a2[maxn];
double Judge(double x) {
double sum1 = 0.0, sum2 = 0.0, sum3 = 0.0;
for (int i = 1; i <= n; i++) a2[i] = a[i] - x;
for (int i = 1; i <= n; i++) {
sum1 = max(sum1 + a2[i], a2[i]);
sum3 = max(sum3, sum1);
}
for (int i = 1; i <= n; i++) a2[i] = a2[i] * (-1.0);
for (int i = 1; i <= n; i++) {
sum2 = max(sum2 + a2[i], a2[i]);
sum3 = max(sum3, sum2);
}
return sum3;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lf", &a[i]);
double l = -40000.0, r = 40000.0;
while (r - l > EXP) {
double mid = (l + l + r) / 3.0;
double mmid = (l + r + r) / 3.0;
if (Judge(mid) >= Judge(mmid))
l = mid;
else
r = mmid;
}
printf("%.13lf\n", min(Judge(l), Judge(r)));
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("", on)
using namespace std;
const long long maxn = 5e5 + 13;
const long long INF = 2e9 + 13;
const long long MOD = 1e9 + 7;
double a[maxn];
double f(double x, long long n) {
vector<double> pref(n), b(n);
double minS = INF, maxS = -INF;
pref[0] = a[1] - x;
for (long long i = 1; i <= n; ++i)
b[i - 1] = a[i] - x, minS = min(a[i] - x, minS), maxS = max(maxS, a[i] - x);
for (long long i = 1; i < n; ++i) pref[i] = pref[i - 1] + b[i];
double minl = 0;
for (long long i = 0; i < n; ++i) {
maxS = max(maxS, pref[i] - minl);
minl = min(pref[i], minl);
}
minl = 0;
for (long long i = 0; i < n; ++i) {
minS = min(minS, pref[i] - minl);
minl = max(minl, pref[i]);
}
return max(abs(minS), abs(maxS));
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
double maxe = 0;
for (long long i = 1; i <= n; ++i) cin >> a[i], maxe = max(maxe, abs(a[i]));
double l = -maxe - 1, r = maxe + 1;
for (long long i = 1; i <= 100; ++i) {
double x1 = l + (r - l) / 3;
double x2 = l + (2 * (r - l)) / 3;
double f1 = f(x1, n);
double f2 = f(x2, n);
if (f1 > f2)
l = x1;
else
r = x2;
}
cout << fixed << setprecision(10) << f(l, n) << "\n";
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC target("avx,avx2,fma")
using namespace std;
template <typename T>
int len(const T& a) {
return a.size();
}
using ll = long long;
constexpr int N = 2e5 + 10;
constexpr long double EPS = 1e-7;
int n;
int a[N];
long double Check(long double x) {
static long double new_a[N];
long double bestmin = 1e8, curmin = 0;
long double bestmax = -1e8, curmax = 0;
for (int i = 0; i < n; ++i) {
new_a[i] = (long double)a[i] - x;
curmax = max((long double)0, curmax + new_a[i]);
bestmax = max(bestmax, curmax);
curmin = min((long double)0, curmin + new_a[i]);
bestmin = min(bestmin, curmin);
}
return max(abs(bestmin), abs(bestmax));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
long double l = -1e4 - 10, r = 1e4 + 10;
for (int step = 0; step < 200; ++step) {
long double m1 = l + (r - l) / 3.0;
long double m2 = r - (r - l) / 3.0;
if (Check(m1) - Check(m2) > EPS) {
l = m1;
} else {
r = m2;
}
}
printf("%.6lf", (double)Check(l));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double maxi, mini, inf = 1e10, ma[200010], mi[200010], L, R, mid, eps = 1e-7,
now, las;
int n, a[200010], f = 0;
double work(double val) {
maxi = -inf, mini = inf;
for (int i = 1; i <= n; i++) {
ma[i] = max(ma[i - 1], 0.0) + a[i] + val,
mi[i] = min(mi[i - 1], 0.0) + a[i] + val;
maxi = max(ma[i], maxi), mini = min(mi[i], mini);
}
if (abs(maxi) > abs(mini))
return maxi;
else
return mini;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
L = -1e5, R = 1e5;
while (1) {
mid = (L + R) / 2;
now = work(mid);
if (f && abs(now - las) < eps) break;
f = 1;
las = now;
if (now > 0)
R = mid;
else
L = mid;
}
printf("%.6lf", abs(work(L)));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long INF = mod * mod;
const long double eps = 1e-6;
const long double pi = acos(-1.0);
int n;
vector<long double> a;
long double calc(long double x) {
vector<long double> ra(n + 1);
for (int i = 0; i < n; i++) {
ra[i + 1] = ra[i] + a[i] - x;
}
long double ret = 0;
long double mi = 0, ma = 0;
for (int i = 1; i <= n; i++) {
ret = max(ret, abs(ra[i] - mi));
ret = max(ret, abs(ma - ra[i]));
mi = min(mi, ra[i]);
ma = max(ma, ra[i]);
}
return ret;
}
void solve() {
cout << fixed << setprecision(7);
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++) cin >> a[i];
long double le = -10000, ri = 10000;
for (int aa = 0; aa < 100; aa++) {
long double l = (le * 2 + ri) / 3.0;
long double r = (le + 2 * ri) / 3.0;
long double cl = calc(l), cr = calc(r);
if (cl < cr)
ri = r;
else
le = l;
}
cout << calc(le) << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<double> v;
int n;
double f(double x) {
double mine = 0, mini = 0, maxi = 0;
for (int i = 0; i < n; ++i) {
mine += v[i] - x;
mini = min(mini, mine);
maxi = max(maxi, mine);
}
return abs(maxi - mini);
}
int main() {
scanf("%d", &n);
v.resize(n);
for (int i = 0; i < n; ++i) scanf("%lf", &v[i]);
int cnt = 0;
double l = -1e5 + 100, r = 1e5 + 100;
while (cnt < 300) {
double left = (r + 2 * l) / 3;
double right = (2 * r + l) / 3;
if (f(left) < f(right))
r = right;
else
l = left;
++cnt;
}
printf("%.10f\n", f(l));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int A[233333], n;
double tA[233333], dp[233333], mr[233333];
int dcmp(double x) {
if (x < -1e-11)
return -1;
else if (x > 1e-11)
return 1;
else
return 0;
}
double calc(double x) {
for (int i = 0; i < n; ++i) {
tA[i] = A[i] - x;
}
dp[0] = mr[0] = max(0.0, tA[0]);
for (int i = 1; i < n; ++i) {
mr[i] = max(0.0, tA[i] + mr[i - 1]);
dp[i] = max(dp[i - 1], mr[i]);
}
double y1 = dp[n - 1];
for (int i = 0; i < n; ++i) {
tA[i] = x - A[i];
}
dp[0] = mr[0] = max(0.0, tA[0]);
for (int i = 1; i < n; ++i) {
mr[i] = max(0.0, tA[i] + mr[i - 1]);
dp[i] = max(dp[i - 1], mr[i]);
}
double y2 = dp[n - 1];
return max(y1, y2);
}
int main() {
while (~scanf("%d", &n)) {
for (int i = 0; i < n; ++i) {
scanf("%d", &A[i]);
}
double L = -2e4, R = 2e4;
while (dcmp(L - R)) {
double m1 = L * 2 / 3 + R / 3;
double m2 = L / 3 + R * 2 / 3;
if (calc(m1) < calc(m2))
R = m2;
else
L = m1;
}
printf("%.9f\n", calc((L + R) / 2.0));
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int a[200000], n;
pair<double, double> go(double x) {
double sum1 = 0, sum2 = 0, mx1 = 0, mx2 = 0;
for (int i = 0; i < (int)(n); i++) {
sum1 += a[i] - x;
if (sum1 < 0) sum1 = 0;
mx1 = max(mx1, sum1);
sum2 += a[i] - x;
if (sum2 > 0) sum2 = 0;
mx2 = min(mx2, sum2);
}
return make_pair(mx1, mx2);
}
int main() {
int mx = INT_MIN, mn = INT_MAX;
cin >> n;
for (int i = 0; i < (int)(n); i++) {
scanf("%d", &a[i]);
mx = max(mx, a[i]);
mn = min(mn, a[i]);
}
double l = mn, r = mx, ans = l;
for (int i = 0; i < (int)(100); i++) {
double m = (r + l) / 2.0;
pair<double, double> qq = go(m);
if (fabs(fabs(qq.first) - fabs(qq.second)) < 1e-9) {
ans = m;
break;
} else if (fabs(qq.first) > fabs(qq.second))
l = m, ans = m;
else
r = m, ans = m;
}
printf("%.9lf\n", fabs(go(ans).first));
}
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
long double ans = 0;
long double check(vector<long long>& mas, long double& x) {
long long n = mas.size();
long double mx1 = mas[0] - x, sum = 0, min_sum = 0;
for (long long r = 0; r < n; r++) {
sum += mas[r] - x;
mx1 = max(mx1, sum - min_sum);
min_sum = min(min_sum, sum);
}
long double mx2 = mas[0] - x;
sum = 0;
min_sum = 0;
for (long long r = 0; r < n; r++) {
sum += mas[r] - x;
mx2 = min(mx2, sum - min_sum);
min_sum = max(min_sum, sum);
}
return max(abs(mx1), abs(mx2));
}
void solve() {
long long n;
cin >> n;
vector<long long> mas(n);
for (long long i = 0; i < n; i++) {
cin >> mas[i];
}
long double l = -10001, r = 10001;
for (long long i = 0; i < 100; i++) {
long double lm = (2 * l + r) / 3;
long double rm = (l + 2 * r) / 3;
long double a = check(mas, lm), b = check(mas, rm);
if (a > b) {
l = lm;
ans = a;
} else {
r = rm;
ans = b;
}
}
cout << fixed << setprecision(20) << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t;
t = 1;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9 + 7;
const long long INF = (long long)1e18 + 7;
const int N = 1234567;
const int M = 1010;
int n;
double a[N], b[N];
void readInput() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
}
double F(double x) {
double sum = 0;
double smax = 0, smin = 0;
double res = 0;
for (int i = 1; i <= n; i++) {
b[i] = a[i] - x;
}
for (int i = 1; i <= n; i++) {
sum += b[i];
smax = max(smax, sum);
smin = min(smin, sum);
res = max(res, max(abs(sum - smax), abs(sum - smin)));
}
return res;
}
void Solve() {
double left = -1e9, right = 1e9;
for (int it = 0; it < 200; it++) {
double x1 = left + (right - left) / 3.0;
double x2 = right - (right - left) / 3.0;
if (F(x1) > F(x2)) {
left = x1;
} else {
right = x2;
}
}
cout << F(left) << '\n';
}
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
cout.precision(10);
cout << fixed;
readInput();
Solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 2e5 + 5;
inline long long read() {
long long ans = 0;
char ch = getchar(), last = ' ';
while (!isdigit(ch)) {
last = ch;
ch = getchar();
}
while (isdigit(ch)) {
ans = ans * 10 + ch - '0';
ch = getchar();
}
if (last == '-') ans = -ans;
return ans;
}
inline void write(long long x) {
if (x < 0) x = -x, putchar('-');
if (x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, a[maxn];
double calc(double x) {
double Min = 0, Max = 0, ans = 0;
for (int i = 1; i <= n; ++i) {
Min = Min + a[i] - x > 0 ? 0 : Min + a[i] - x;
Max = Max + a[i] - x < 0 ? 0 : Max + a[i] - x;
ans = max(ans, max(Max, -Min));
}
return ans;
}
int main() {
n = read();
for (int i = 1; i <= n; ++i) a[i] = read();
double L = -10000, R = 10000, x;
for (int i = 1; i <= 100; ++i) {
double m1 = L + (R - L) / 3.00;
double m2 = R - (R - L) / 3.00;
if (calc(m1) <= calc(m2))
x = m1, R = m2;
else
x = m2, L = m1;
}
printf("%.8lf\n", calc(x));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int INF = (1 << 30) - 1;
const int MAXN = 200010;
int n;
int A[MAXN];
double B[MAXN];
double sum[MAXN];
double Cal(double v) {
for (int i = 1; i <= n; ++i) {
B[i] = 1.0 * A[i] - v;
}
long long MAX_LL = 1LL << 60;
double sum_max = 0, sum_min = 0;
double ans_max = 0, ans_min = MAX_LL;
for (int i = 1; i <= n; ++i) {
if (sum_min > 0) sum_min = 0;
if (sum_max < 0) sum_max = 0;
sum_max += B[i];
sum_min += B[i];
ans_max = max(ans_max, sum_max);
ans_min = min(ans_min, sum_min);
}
if (ans_max < 0) ans_max = -ans_max;
if (ans_min < 0) ans_min = -ans_min;
return max(ans_max, ans_min);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &A[i]);
}
double l = -10000.0, r = 10000.0;
int cnt = 100;
while (cnt--) {
double mid = ((l) + ((r) - (l)) / 2);
double MID = ((mid) + ((r) - (mid)) / 2);
double A1 = Cal(mid);
double A2 = Cal(MID);
if (A1 < A2)
r = MID;
else
l = mid;
}
printf("%.12f\n", Cal(l));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e5 + 5;
int a[N];
int n;
double f(double x) {
double mn = a[0] - x;
double mx = a[0] - x;
double cur_mn = mn, cur_mx = mx;
for (int i = 1; i < n; i++) {
cur_mx = max(a[i] - x, cur_mx + a[i] - x);
cur_mn = min(a[i] - x, cur_mn + a[i] - x);
mx = max(mx, cur_mx);
mn = min(mn, cur_mn);
}
return max(fabs(mn), mx);
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
double l = -1e9, r = 1e9;
for (int i = 0; i < 500; i++) {
double m1 = l + (r - l) / 3.0;
double m2 = r - (r - l) / 3.0;
if (f(m1) > f(m2)) {
l = m1;
} else
r = m2;
}
cout << fixed;
cout << setprecision(15) << f(l) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const long long mod = 1000000007;
const long long maxn = 200005;
const double epsilon = 1e-11;
double a[maxn], mn, mp;
long long n;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << setprecision(12) << fixed;
while (cin >> n) {
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
long long t = 100;
double low = *(std::min_element(a, a + n));
double high = *(std::max_element(a, a + n));
double mid;
while (t--) {
mid = (low + high) / 2.0;
mn = mod, mp = -mod;
double cn = 0, cp = 0, tmp;
for (int i = 0; i < n; ++i) {
tmp = a[i] - mid;
cp = max(cp + tmp, tmp);
cn = min(cn + tmp, tmp);
mp = max(cp, mp);
mn = min(cn, mn);
}
if (mn + mp > 0) {
low = mid;
} else {
high = mid;
}
}
mn = mod, mp = -mod;
double cn = 0, cp = 0, tmp;
for (int i = 0; i < n; ++i) {
tmp = a[i] - mid;
cp = max(cp + tmp, tmp);
cn = min(cn + tmp, tmp);
mp = max(cp, mp);
mn = min(cn, mn);
}
cout << max(mp, -mn) << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 100 * 1000;
long double l[N];
long double Maks(long double dod, int n) {
int i;
long double as, mi, w;
mi = 0;
w = -10000000000000;
as = 0;
for (i = 1; i <= n; ++i) {
as += (l[i] - dod);
w = max(w, as - mi);
mi = min(mi, as);
}
return w;
}
long double Minim(long double dod, int n) {
int i;
long double as, ma, w;
ma = 0;
w = 10000000000000;
as = 0;
for (i = 1; i <= n; ++i) {
as += (l[i] - dod);
w = min(w, as - ma);
ma = max(ma, as);
}
return w;
}
long double TS(long double p, long double k, int n) {
int i;
long double s1, s2, w1, w2;
for (i = 1; i <= 100; ++i) {
s1 = p + ((k - p) / 3);
s2 = k - ((k - p) / 3);
w1 = max(abs(Maks(s1, n)), abs(Minim(s1, n)));
w2 = max(abs(Maks(s2, n)), abs(Minim(s2, n)));
if (w1 < w2)
k = s2;
else
p = s1;
}
return p;
}
void WczytajLiczby(int &n) {
int i;
cin >> n;
for (i = 1; i <= n; ++i) cin >> l[i];
}
void Weakness() {
int n;
long double w;
WczytajLiczby(n);
w = TS(-10000, 10000, n);
w = max(abs(Minim(w, n)), abs(Maks(w, n)));
cout << fixed << setprecision(10) << w << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
Weakness();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using db = long double;
using ll = long long;
using ull = unsigned long long;
using pII = pair<int, int>;
using pLL = pair<ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2>
inline void chadd(T1 &x, T2 y, int Mod = mod) {
x += y;
while (x >= Mod) x -= Mod;
while (x < 0) x += Mod;
}
template <class T1, class T2>
inline void chmax(T1 &x, T2 y) {
if (x < y) x = y;
}
template <class T1, class T2>
inline void chmin(T1 &x, T2 y) {
if (x > y) x = y;
}
inline int nextInt() {
int x;
cin >> x;
return x;
}
void rd() {}
template <class T, class... Ts>
void rd(T &arg, Ts &...args) {
cin >> arg;
rd(args...);
}
void err() {
cout << "\033[39;0m"
<< "\n";
}
template <class T, class... Ts>
void err(const T &arg, const Ts &...args) {
cout << arg << ' ';
err(args...);
}
template <template <typename...> class T, typename t, typename... A>
void err(const T<t> &arg, const A &...args) {
for (auto &v : arg) cout << v << ' ';
err(args...);
}
void ptt() { cout << "\n"; }
template <class T, class... Ts>
void ptt(const T &arg, const Ts &...args) {
cout << ' ' << arg;
ptt(args...);
}
template <class T, class... Ts>
void pt(const T &arg, const Ts &...args) {
cout << arg;
ptt(args...);
}
void pt() {}
template <template <typename...> class T, typename t, typename... A>
void pt(const T<t> &arg, const A &...args) {
for (int i = 0, sze = arg.size(); i < sze; ++i)
cout << arg[i] << " \n"[i == sze - 1];
pt(args...);
}
inline ll qpow(ll base, ll n) {
assert(n >= 0);
ll res = 1;
while (n) {
if (n & 1) res = res * base % mod;
base = base * base % mod;
n >>= 1;
}
return res;
}
constexpr int N = 2e5 + 10;
constexpr db eps = 1e-8L;
int n, a[N];
db S[N];
db gao(db x) {
db res = 0;
db Min = 0, Max = 0;
for (int i = 1; i <= n; ++i) {
S[i] = S[i - 1] + a[i] - x;
chmax(res, fabs(S[i] - Min));
chmax(res, fabs(S[i] - Max));
chmax(Min, S[i]);
chmin(Max, S[i]);
}
return res;
}
void run() {
int ok = 1;
for (int i = 1; i <= n; ++i) {
rd(a[i]);
if (i > 1 && a[i] != a[i - 1]) ok = 0;
}
if (ok) return pt(0.0);
int cnt = 210;
db l = -1e4, r = 1e4, res = 2e9;
while (cnt--) {
db a = l + (r - l) / 3;
db b = l + (r - l) / 3 * 2;
db L = gao(a), R = gao(b);
if (L <= R) {
chmin(res, L);
r = b;
} else {
l = a;
}
}
return pt(res);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout << fixed << setprecision(20);
while (cin >> n) run();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
const long double EPS = 1e-12;
int mn, mx, n;
long double p[N];
long double f(long double x) {
mn = mx = 0;
for (int i = 1; i <= n; i++) {
if (p[i] - x * i > p[mx] - x * mx) mx = i;
if (p[i] - x * i < p[mn] - x * mn) mn = i;
}
return (p[mx] - x * mx) - (p[mn] - x * mn);
}
long double l, r, m1, m2, f1, f2;
int a[N];
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
p[i + 1] = p[i] + a[i];
}
l = -120000;
r = 120000;
while (clock() < 1200) {
m1 = (l + l + r) / 3.0;
m2 = (l + r + r) / 3.0;
f1 = f(m1);
f2 = f(m2);
if (f1 < f2)
r = m2;
else
l = m1;
}
cout.precision(10);
cout << fixed << f((l + r) / 2.0);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
double a[200090];
double eps = 5e-12;
double cal(double x) {
double sum = 0, MAX = 0, MIN = 0;
for (int i = 0; i < n; i++) {
sum += a[i] - x;
MAX = max(MAX, sum);
MIN = min(MIN, sum);
}
return MAX - MIN;
}
int main(int argc, char* argv[]) {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%lf", &a[i]);
double left = -20000, right = 20000;
while (right - left > eps) {
double L = (left * 2 + right) / 3, R = (left + right * 2) / 3;
if (cal(L) > cal(R))
left = L;
else
right = R;
}
printf("%.15lf\n", cal(left));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mn = 200010;
const double inf = 1e18;
int a[mn], n;
double check(double x) {
double sum = 0, ret = 0, Min = 0, Max = 0;
for (int i = 1; i <= n; i++) {
sum += a[i] - x;
ret = max(ret, fabs(sum - Min));
ret = max(ret, fabs(sum - Max));
Min = min(Min, sum);
Max = max(Max, sum);
}
return ret;
}
int main() {
double l = 0, r;
int cnt = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
l = min(l, -fabs(a[i]));
}
r = -l;
for (int i = 1; i <= 300; i++) {
double ml = (r - l) / 3.0 + l, mr = (r - l) / 3.0 * 2.0 + l;
if (check(ml) < check(mr))
r = mr;
else
l = ml;
}
cout << fixed << setprecision(6) << check((l + r) * 0.5);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
double arr[200005], temp[200005], dp[200005];
double x;
double minuss, pluss;
void check(double x) {
minuss = 10000.0, pluss = -10000.0;
for (int i = 1; i <= n; i++) dp[i] = 0.0;
for (int i = 1; i <= n; i++) temp[i] = arr[i] - x;
for (int i = 1; i <= n; i++) dp[i] = max(temp[i], dp[i - 1] + temp[i]);
for (int i = 1; i <= n; i++) pluss = max(pluss, dp[i]);
for (int i = 1; i <= n; i++) dp[i] = 0.0;
for (int i = 1; i <= n; i++) dp[i] = min(temp[i], dp[i - 1] + temp[i]);
for (int i = 1; i <= n; i++) minuss = min(minuss, dp[i]);
minuss = abs(minuss);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lf", &arr[i]);
cout.precision(10);
double l = -10000.0;
double r = 10000.0;
int cnt = 0;
while (l <= r && cnt < 100) {
double mid = (l + r) / 2.0;
check(mid);
if (pluss > minuss)
l = mid;
else if (minuss > pluss)
r = mid;
else
break;
cnt++;
}
cout << max(minuss, pluss) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200001;
int n;
int v[MAX];
double first(double x) {
double ret = 0;
double s1 = 0, s2 = 0;
for (int i = 0; i < n; i++) {
s1 += v[i] - x;
ret = max(ret, s1);
s1 = max(s1, 0.0);
s2 += v[i] - x;
ret = max(ret, -s2);
s2 = min(s2, 0.0);
}
return ret;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &v[i]);
double l = -1e9, r = 1e9;
int nbr(150);
while (r - l > 1e-12) {
if (!nbr) break;
nbr--;
double m1 = (2.0 * l + r) / 3.0;
double m2 = (l + 2.0 * r) / 3.0;
if (first(m1) > first(m2))
l = m1;
else
r = m2;
}
cout << fixed << setprecision(12) << first(l) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
double a[maxn], b[maxn];
int n;
double mcs() {
double ans = 0, cur = 0;
for (int i = 0; i < n; i++) {
cur += b[i];
if (cur > ans) ans = cur;
if (cur < 0) cur = 0;
}
return ans;
}
void calc(double mid, double &a1, double &a2) {
for (int i = 0; i < n; i++) b[i] = a[i] - mid;
a1 = mcs();
for (int i = 0; i < n; i++) b[i] = mid - a[i];
a2 = mcs();
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%lf", &a[i]);
double l = -1e9, r = 1e9, ans = 1e18;
for (int i = 0; i < 100; i++) {
double mid = (l + r) / 2, a1, a2;
calc(mid, a1, a2);
if (a1 > a2)
l = mid;
else
r = mid;
ans = min(ans, max(a1, a2));
}
printf("%.12f\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct __s {
__s() {
if (1) {
ios_base::Init i;
cin.sync_with_stdio(0);
cin.tie(0);
}
}
~__s() {
if (!1)
fprintf(stderr, "Execution time: %.3lf s.\n",
(double)clock() / CLOCKS_PER_SEC);
long long n;
cin >> n;
}
} __S;
long long n;
double a[211111];
double mx() {
double ans = a[0];
double sum = 0;
double min_sum = 0;
for (long long r = 0; r < (long long)(n); r++) {
sum += a[r];
ans = max(ans, sum - min_sum);
min_sum = min(min_sum, sum);
}
return ans;
}
int main(void) {
cin >> n;
for (long long i = 0; i < (long long)(n); i++) {
long long x;
cin >> x;
a[i] = x;
}
double l = 2e4;
double r = -2e4;
for (long long _ = 0; _ < (long long)(60); _++) {
double x = (l + r) / 2.0;
for (long long i = 0; i < (long long)(n); i++) {
a[i] += x;
}
double mx1 = mx();
for (long long i = 0; i < (long long)(n); i++) {
a[i] *= -1;
}
double mx2 = mx();
for (long long i = 0; i < (long long)(n); i++) {
a[i] *= -1;
a[i] -= x;
}
if (mx1 >= mx2) {
l = x;
} else {
r = x;
}
}
double x = l;
for (long long i = 0; i < (long long)(n); i++) {
a[i] += x;
}
double mx1 = mx();
for (long long i = 0; i < (long long)(n); i++) {
a[i] *= -1;
}
double mx2 = mx();
cout << fixed << setprecision(10) << max(mx1, mx2) << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
int len(const T& a) {
return a.size();
}
using ll = long long;
constexpr long double EPS = 1e-7;
long double Check(const vector<long double>& a, long double x) {
vector<long double> new_a(a.size());
long double bestmin = 1e8, curmin = 0;
long double bestmax = -1e8, curmax = 0;
for (int i = 0; i < len(a); ++i) {
new_a[i] = (long double)a[i] - x;
curmax = max((long double)0, curmax + new_a[i]);
bestmax = max(bestmax, curmax);
curmin = min((long double)0, curmin + new_a[i]);
bestmin = min(bestmin, curmin);
}
return max(abs(bestmin), abs(bestmax));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(6);
int n;
cin >> n;
vector<long double> a(n);
for (auto& i : a) cin >> i;
long double l = -1e4 - 10, r = 1e4 + 10;
for (int step = 0; step < 200; ++step) {
long double m1 = l + (r - l) / 3.0;
long double m2 = r - (r - l) / 3.0;
if (Check(a, m1) - Check(a, m2) > EPS) {
l = m1;
} else {
r = m2;
}
}
cout << Check(a, l);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int a[200005], n;
double solve(double x) {
double sum = 0, maxisum = 0, minisum = 0;
for (int i = 0; i < n; i++) {
sum += (a[i] - x);
if (sum < 0) sum = 0;
maxisum = max(maxisum, sum);
}
sum = 0;
for (int i = 0; i < n; i++) {
sum += (a[i] - x);
if (sum > 0) sum = 0;
minisum = min(minisum, sum);
}
return max(maxisum, -minisum);
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
double esq = -1e18, dir = 1e18;
int iter = 500;
while (iter--) {
double jump = (dir - esq) / 3;
double t1 = esq + jump;
double t2 = dir - jump;
double x1 = solve(t1);
double x2 = solve(t2);
if (x1 > x2)
esq = t1;
else
dir = t2;
}
printf("%.6f\n", solve(esq));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
int BIT(int i, T s) {
return (s >> i) & 1;
}
template <class T>
T ONBIT(int i, T s) {
return s | (T(1) << i);
}
template <class T>
T offbit(T s, int i) {
return s & (~(T(1) << i));
}
template <class T>
int cntbit(T s) {
return __builtin_popcount(s);
}
template <class T>
T gcd(T a, T b) {
T r;
int dem = 0;
while (b != 0) {
r = a % b;
dem++;
a = b;
b = r;
}
cout << dem << endl;
return a;
}
template <class T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
const int maxv = 500;
const int maxe = maxv * maxv;
const int inf = (int)1e9 + 5;
const long long linf = (long long)1e16 + 5;
const int dr[] = {-1, 0, +1, 0};
const int dc[] = {0, +1, 0, -1};
const long double eps = 1e-9;
const char IN[] = "_.in";
const char OUT[] = "_.out";
const int maxn = 111111;
const int base = 1e9 + 7;
int test;
long long n;
double l, r, m1, m2;
double a[200111], b[200111];
double nick(double x) {
for (int i = (1); i <= (n); i++) b[i] = a[i] - x;
double sum = 0.0;
double res = 0.0;
double mi = 0, ma = 0;
for (int i = (1); i <= (n); i++) {
sum = sum + b[i];
res = max(res, abs(sum - mi));
res = max(res, abs(sum - ma));
mi = min(mi, sum);
ma = max(ma, sum);
}
return res;
}
int smd(int u) {
int r = 0;
while (u > 0) {
r = r + u % 10;
u = u / 10;
}
return r;
}
int main() {
cin >> n;
for (int i = (1); i <= (n); i++) cin >> a[i];
l = -10005;
r = 10005;
for (int i = (1); i <= (200); i++) {
m1 = (l + l + r) / 3;
m2 = (l + r + r) / 3;
if (nick(m1) < nick(m2))
r = m2;
else
l = m1;
}
cout << fixed << setprecision(7) << nick(l) << endl;
return 0;
}
|
#include <bits/stdc++.h>
int T;
double a[200005];
double f(double x) {
int i;
double m = 0, n = 0, s[200005] = {0};
for (i = 1; i <= T; i++) {
s[i] = s[i - 1] + a[i - 1] - x;
m = m > s[i] ? m : s[i];
n = n < s[i] ? n : s[i];
}
return m - n;
}
int main() {
int i, t = 200;
double l = -1e5, m, n, r = 1e5;
scanf("%d", &T);
for (i = 0; i < T; i++) scanf("%lf", &a[i]);
while (t--) {
m = l + (r - l) / 3;
n = r - (r - l) / 3;
if (f(m) < f(n))
r = n;
else
l = m;
}
printf("%.15lf\n", f(m));
}
|
#include <bits/stdc++.h>
using namespace std;
double a[200010];
double w[200010];
int N;
double check(double x) {
double ans = 0, tmp = 0;
for (int i = 0; i < N; i++) {
if (tmp < 0) tmp = 0;
tmp += (a[i] - x);
ans = max(ans, fabs(tmp));
}
tmp = 0;
for (int i = 0; i < N; i++) {
if (tmp > 0) tmp = 0;
tmp += (a[i] - x);
ans = max(ans, fabs(tmp));
}
return ans;
}
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%lf", &a[i]);
}
double l = -1e4, r = 1e4;
for (int i = 0; i < 100; i++) {
double d = (r - l) / 3;
double m1 = l + d, m2 = r - d;
double c1 = check(m1), c2 = check(m2);
if (c1 > c2)
l = m1;
else
r = m2;
}
printf("%.9f\n", check((l + r) / 2));
return 0;
}
|
#include <bits/stdc++.h>
struct compMinPQ {
bool operator()(const int &a, const int &b) { return a > b; }
};
using namespace std;
double a[200005];
int N;
double f(double x) {
int i, j, k;
for (i = 1; i <= N; i++) a[i] = a[i] - x;
double max_so_far = a[1], max_ending = a[1];
for (i = 2; i <= N; i++) {
max_ending = max(a[i], max_ending + a[i]);
max_so_far = max(max_so_far, max_ending);
}
double min_so_far = a[1], min_ending = a[1];
for (i = 2; i <= N; i++) {
min_ending = min(a[i], min_ending + a[i]);
min_so_far = min(min_ending, min_so_far);
}
for (i = 1; i <= N; i++) a[i] = a[i] + x;
return max(abs(max_so_far), abs(min_so_far));
}
int main() {
int i, j, k;
cin >> N;
for (i = 1; i <= N; i++) cin >> a[i];
double low = -10005;
double high = 10005;
double esp = 1e-13;
double ans;
for (i = 0; i < 200; i++) {
double mid1 = low + (high - low) / 3.0;
double mid2 = high - (high - low) / 3.0;
if (f(mid1) - f(mid2) > esp) {
ans = mid2;
low = mid1 + esp;
} else {
ans = mid1;
high = mid2 - esp;
}
}
ans = f(ans);
printf("%.12f\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
int n, a[MAXN];
double getWeakness(double x) {
double cur = 0.0, maxv = 0.0, minv = 0.0, ret = 0.0;
for (int i = 1; i <= n; ++i) {
cur += a[i] + x;
ret = max(ret, fabs(maxv - cur));
ret = max(ret, fabs(cur - minv));
maxv = max(maxv, cur);
minv = min(minv, cur);
}
return ret;
}
int main() {
int i;
double l, r, mid1, mid2;
scanf("%d", &n);
for (i = 1; i <= n; ++i) scanf("%d", &a[i]);
l = -10000.0, r = 10000.0;
for (i = 1; i <= 100; ++i) {
mid1 = l + (r - l) / 3.0;
mid2 = r - (r - l) / 3.0;
if (getWeakness(mid1) > getWeakness(mid2))
l = mid1;
else
r = mid2;
}
printf("%.15f\n", getWeakness(r));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long double a[200100], b[200100];
int n;
long double tru(long double x) {
int k;
long double max = 0, pos = 0, neg = 0;
for (k = 1; k <= n; k++) {
b[k] = a[k] + x;
}
for (k = 1; k <= n; k++) {
pos += b[k];
neg += b[k];
if (fabs(pos) > max) max = fabs(pos);
if (fabs(neg) > max) max = fabs(neg);
if (pos < 0) pos = 0;
if (neg > 0) neg = 0;
}
return max;
}
int main() {
int k;
cin >> n;
long double d = 1e-12, l = -123034, r = 171022, p, q, m;
for (k = 1; k <= n; k++) cin >> a[k];
for (k = 1; k <= 100; k++) {
m = (l + r) / 2;
p = tru(m);
q = tru(m + d);
if (p > q)
l = m;
else
r = m;
}
cout << setprecision(12) << tru(m);
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
double a[200010], s[200010] = {};
double f(double x) {
double mi = 0, mx = 0, ret = 0;
for (int i = 1; i <= n; ++i) {
s[i] = s[i - 1] + a[i] - x;
ret = max(ret, fabs(s[i] - mi));
ret = max(ret, fabs(s[i] - mx));
mi = min(mi, s[i]);
mx = max(mx, s[i]);
}
return ret;
}
int main() {
scanf("%d", &n);
for (int i = 1, x; i <= n; ++i) {
scanf("%d", &x);
a[i] = x;
}
double l = -1000000, r = 1000000;
for (int i = 0; i < 100; ++i) {
double m1 = l + (r - l) * 0.33;
double m2 = l + (r - l) * 0.67;
double r1 = f(m1);
double r2 = f(m2);
if (r1 > r2)
l = m1;
else
r = m2;
}
printf("%.12f\n", f((l + r) / 2));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> v(2 * 100000);
vector<double> vv(2 * 100000);
inline void calc(int l, double m, double &p, double &n) {
int x;
for (x = 0; x < l; x++) vv[x] = v[x] * 1.0 - m;
double mx = 0, sm = 0;
for (x = 0; x < l; x++) {
sm += vv[x];
if (sm > mx) mx = sm;
if (sm < 0) sm = 0;
}
p = mx;
for (x = 0; x < l; x++) vv[x] = -vv[x];
mx = sm = 0;
for (x = 0; x < l; x++) {
sm += vv[x];
if (sm > mx) mx = sm;
if (sm < 0) sm = 0;
}
n = mx;
}
int main() {
scanf("%d", &n);
;
int x;
for (x = 0; x < n; x++) scanf("%d", &v[x]);
;
double l = *min_element(v.begin(), v.begin() + n);
double r = *max_element(v.begin(), v.begin() + n);
double eps = 1e-6;
double ans;
for (x = 1; x <= 100; x++) {
double m = (l + r) / 2.0;
double pos, neg;
calc(n, m, pos, neg);
if (pos > neg)
l = m;
else if (pos < neg)
r = m;
else if (abs(pos - neg) == eps) {
ans = pos;
break;
}
}
printf("%.7lf", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void Max(T &a, T b) {
if (a < b) a = b;
}
template <class T>
inline void Min(T &a, T b) {
if (a > b) a = b;
}
int a[200005];
double cal(double first, int n) {
double s = 0, mi = 0, mx = 0;
for (int i = 1; i <= n; i++) {
s += a[i] - first;
Min(mi, s);
Max(mx, s);
}
return abs(mx - mi);
}
int main() {
int T, i, j, k, m, n;
scanf("%d", &n);
double l = -1e9, r = 1e9;
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int it = 0; it < 100; it++) {
double x1 = (11 * l + 10 * r) / 21;
double x2 = (10 * l + 11 * r) / 21;
double y1 = cal(x1, n), y2 = cal(x2, n);
if (y1 >= y2)
l = x1;
else
r = x2;
}
printf("%.9lf\n", cal(l, n));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e8;
const int maxn = 2e5 + 100;
int n, a[maxn];
double check(double x) {
double minv = 0.0, maxv = 0.0, ret = 0.0;
for (int i = 1; i <= n; i++) {
double tmp = a[i] - x;
maxv += tmp, minv += tmp;
ret = max(ret, abs(maxv));
ret = max(ret, abs(minv));
if (maxv < 0) maxv = 0;
if (minv > 0) minv = 0;
}
return ret;
}
int main() {
cin >> n;
int maxv = -inf, minv = inf;
for (int i = 1; i <= n; i++) {
cin >> a[i];
maxv = max(maxv, a[i]);
minv = min(minv, a[i]);
}
double l = minv, r = maxv, ans;
for (int i = 1; i <= 80; i++) {
double tmp = (r - l) / 3.0;
double x = l + tmp;
double y = l + tmp * 2;
if (check(x) < check(y))
r = y;
else
l = x;
}
printf("%.10f", check(l));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mn = 2e5 + 10;
int n;
long double a[mn], p[mn];
long double val(long double x) {
long double sm = 0, lar = 0, ans = 0;
for (int i = 1; i <= n; i++) {
p[i] = p[i - 1] + a[i] - x;
sm = min(sm, p[i]);
lar = max(lar, p[i]);
ans = max(ans, max(abs(p[i] - sm), abs(p[i] - lar)));
}
return ans;
}
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
long double l = -20000, r = 20000;
while (r - l > 1e-12) {
long double a = (2 * l + r) / 3, b = (2 * r + l) / 3;
if (val(a) < val(b))
r = b;
else
l = a;
}
printf("%.100Lf", val((l + r) / 2));
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200000;
int sum[N + 10];
int main() {
int n, num;
double ans;
while (~scanf("%d", &n)) {
memset(sum, 0, sizeof(sum));
scanf("%d", &num);
sum[0] = num;
for (int i = 1; i < n; i++) {
scanf("%d", &num);
sum[i] = sum[i - 1] + num;
}
double left, right, midL, midR;
double a, b, c, d;
double flag;
left = -10000, right = 10000;
int Z = 110;
while (Z--) {
midL = (right - left) / 3 + left;
midR = (right - left) * 2 / 3 + left;
a = b = c = d = 0;
for (int i = 0; i < n; i++) {
flag = sum[i] + midL * (i + 1);
a = max(a, flag);
b = min(b, flag);
flag = sum[i] + midR * (i + 1);
c = max(c, flag);
d = min(d, flag);
}
if (a - b > c - d) {
left = midL;
ans = c - d;
} else {
right = midR;
ans = a - b;
}
}
printf("%.15lf\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
double a[200010], ans, b[200010], c[200010], sum1[200010], sum2[200010];
double f(double x) {
double max1 = -100000.0, max2 = -100000.0;
sum1[0] = sum2[0] = 0.0;
for (int i = 1; i <= n; i++) {
b[i] = a[i] - x;
c[i] = -b[i];
}
for (int i = 1; i <= n; i++) {
sum1[i] = max(sum1[i - 1] + b[i], b[i]);
max1 = max(max1, sum1[i]);
sum2[i] = max(sum2[i - 1] + c[i], c[i]);
max2 = max(max2, sum2[i]);
}
return max(max1, max2);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lf", &a[i]);
if (n == 1)
printf("%.15lf\n", 0);
else {
double L = -20000.0, R = 20000.0;
for (int i = 1; i <= 200; i++) {
double mid1 = L + (R - L) / 3.0, mid2 = R - (R - L) / 3.0;
if (f(mid1) < f(mid2)) {
ans = mid1;
R = mid2;
} else {
ans = mid2;
L = mid1;
}
}
printf("%.15lf\n", f(ans));
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void Rd(int &res) {
res = 0;
char p;
int k = 1;
while (p = getchar(), !(p >= '0' && p <= '9') && p != '-')
;
if (p == '-') k = -1, p = getchar();
do {
res = (res << 1) + (res << 3) + (p ^ 48);
} while (p = getchar(), p >= '0');
res *= k;
}
int a[200005], n;
bool gre(double a, double b) { return a - b > 0.0000000001; }
double calc(double pl) {
double Mx = 0, Mi = 0, Mxsum = 0, Misum = 0, bas = 0;
for (int i = 1; i <= n; i++) {
if (gre(bas, Mxsum)) Mxsum = bas;
if (gre(bas, Misum)) Misum = bas;
Mxsum += a[i] - pl;
Misum -= a[i] - pl;
if (gre(Mxsum, Mx)) Mx = Mxsum;
if (gre(Misum, Mi)) Mi = Misum;
}
if (gre(Mi, Mx))
return -1;
else
return Mx;
}
int main() {
Rd(n);
for (int i = 1; i <= n; i++) Rd(a[i]);
double l = -10000, r = 10000, ans = 0;
for (int i = 1; i <= 100; i++) {
double mid = (l + r) / 2;
double lp = calc(mid);
if (gre(lp, 0))
ans = lp, l = mid;
else
r = mid;
}
printf("%.15lf\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double weakness(vector<int> &v, double x) {
double smin = 0, smax = 0, best = 0, cur;
for (int i = 0; i < v.size(); i++) {
cur = v[i] - x;
smin = min(cur, smin + cur);
smax = max(cur, smax + cur);
best = max({best, abs(smax), abs(smin)});
}
return best;
}
int main() {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
double l = -10000, r = 10000;
for (int i = 0; i < 60; i++) {
double m = (l + r) / 2;
if (weakness(v, m) >= weakness(v, m + 1e-12))
l = m;
else
r = m;
}
printf("%.9lf\n", weakness(v, l));
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200000 + 10;
double a[MAXN];
double dp[MAXN];
double f[MAXN];
int n;
double cal(double x) {
double res = 0;
f[1] = dp[1] = a[1] - x;
res = fabs(dp[1]);
for (int i = 2; i <= n; i++) {
double tmp = a[i] - x;
dp[i] = max(tmp, dp[i - 1] + tmp);
f[i] = min(tmp, tmp + f[i - 1]);
res = max(res, fabs(dp[i]));
res = max(res, fabs(f[i]));
}
return res;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lf", a + i);
double L = -MAXN, R = MAXN;
for (int i = 1; i <= 300; i++) {
double m1 = L + (R - L) / 3;
double m2 = R - (R - L) / 2;
if (cal(m1) < cal(m2)) {
R = m2;
} else {
L = m1;
}
}
printf("%.12f\n", cal(L));
}
|
#include <bits/stdc++.h>
using namespace std;
double a[200011];
int n;
double check(double x) {
for (int i = 1; i <= n; i++) {
a[i] -= x;
}
double mi = min(0.0, a[1]);
double ans = 0;
double ma = max(0.0, a[1]);
double cur = 0;
for (int i = 1; i <= n; i++) {
cur += a[i];
mi = min(mi, cur);
ma = max(ma, cur);
ans = max(ans, cur);
ans = max(ans, fabs(cur - mi));
ans = max(ans, fabs(cur - ma));
}
for (int i = 1; i <= n; i++) {
a[i] += x;
}
return ans;
}
int main() {
double lo = 0;
double hi = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
hi = max(hi, abs(a[i]));
}
lo = -hi;
double mid;
double m1, m2, v1, v2;
for (int g = 0; g <= 200; g++) {
m1 = lo + (hi - lo) / 3;
m2 = lo + 2 * (hi - lo) / 3;
v1 = check(m1);
v2 = check(m2);
if (v1 > v2) {
lo = m1;
} else if (v1 < v2) {
hi = m2;
} else {
lo = m1;
hi = m2;
}
}
printf("%.10f", check(lo));
}
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long powmod(long long a, long long b, long long MOD) {
if (a == 0ll) {
return 0ll;
}
a %= MOD;
long long ans = 1;
while (b) {
if (b & 1) {
ans = ans * a % MOD;
}
a = a * a % MOD;
b >>= 1;
}
return ans;
}
long long poww(long long a, long long b) {
if (a == 0ll) {
return 0ll;
}
long long ans = 1;
while (b) {
if (b & 1) {
ans = ans * a;
}
a = a * a;
b >>= 1;
}
return ans;
}
void Pv(const vector<int> &V) {
int Len = int(V.size());
for (int i = 0; i < Len; ++i) {
printf("%d", V[i]);
if (i != Len - 1) {
printf(" ");
} else {
printf("\n");
}
}
}
void Pvl(const vector<long long> &V) {
int Len = int(V.size());
for (int i = 0; i < Len; ++i) {
printf("%lld", V[i]);
if (i != Len - 1) {
printf(" ");
} else {
printf("\n");
}
}
}
inline long long readll() {
long long tmp = 0, fh = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') fh = -1;
c = getchar();
}
while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar();
return tmp * fh;
}
inline int readint() {
int tmp = 0, fh = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') fh = -1;
c = getchar();
}
while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar();
return tmp * fh;
}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
int n;
int a[maxn];
double b[maxn];
double getv(double x) {
for (int i = 1; i <= n; i++) {
b[i] = a[i] - x;
}
double res = 0;
double sum = 0;
for (int i = 1; i <= n; i++) {
sum += b[i];
res = max(res, sum);
if (sum < 0) sum = 0;
}
sum = 0;
for (int i = 1; i <= n; i++) {
b[i] *= -1;
}
for (int i = 1; i <= n; i++) {
sum += b[i];
res = max(res, sum);
if (sum < 0) sum = 0;
}
return res;
}
int main() {
n = readint();
for (int i = 1; i <= n; i++) {
a[i] = readint();
}
double low = *min_element(a + 1, a + 1 + n);
double high = *max_element(a + 1, a + 1 + n);
for (int rp = 1; rp <= 80; rp++) {
double lmid = (low + low + high) / 3.000000;
double rmid = (low + high + high) / 3.000000;
double mv = getv(lmid);
double mmv = getv(rmid);
if (mv > mmv) {
low = lmid;
} else {
high = rmid;
}
}
printf("%.6f\n", min(getv(low), getv(high)));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, o, re = 0;
double res = 0;
double val = 10000;
int a[1000010], b[1000010];
bool chk[1000010];
double calc(double d) {
double ss = 0, sm = 0, ss2 = 0;
for (int j = 0; j < n; j++) {
if (ss < 0) ss = 0;
if (ss2 > 0) ss2 = 0;
ss += (double)a[j] - d;
ss2 += (double)a[j] - d;
sm = max(sm, max(((ss) < 0 ? -(ss) : (ss)), ((ss2) < 0 ? -(ss2) : (ss2))));
}
return sm;
}
int main() {
int i, j, k, l;
double ll = 0, rr = 20000, mid;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i < 100; i++) {
mid = (ll + rr) / 2;
res = calc(mid - val);
double res2 = calc(mid - val + 1e-12);
if (res < res2)
rr = mid;
else
ll = mid;
}
printf("%.7lf", calc(ll - val));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double a[200010];
int n;
double f(double x) {
double sum = 0, mx = 0, mn = 0;
for (int i = 0; i < n; i++) {
sum += a[i] - x;
mx = max(mx, sum);
mn = min(sum, mn);
}
return (mx - mn);
}
int main() {
while (scanf("%d", &n) == 1) {
for (int i = 0; i < n; i++) cin >> a[i];
double l = -10001, r = 10001, ml, mr, z1, z2;
int x = 0;
while (r - l >= 0.00000000001) {
ml = (l + r) / 2.0;
mr = (ml + r) / 2.0;
z1 = f(mr), z2 = f(ml);
if (z1 < z2)
l = ml;
else
r = mr;
x++;
}
l = (l + r) / 2.0;
cout << fixed << setprecision(10) << f(l) << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
double a[200001];
double weakness(double x) {
double best_sum = 0.0;
double curr_sum = 0.0;
for (int i = 0; i < n; i++) {
curr_sum += a[i] - x;
best_sum = max(best_sum, curr_sum);
if (curr_sum < 0.0) {
curr_sum = 0.0;
}
}
curr_sum = 0.0;
for (int i = 0; i < n; i++) {
curr_sum -= a[i] - x;
best_sum = max(best_sum, curr_sum);
if (curr_sum < 0.0) {
curr_sum = 0.0;
}
}
return best_sum;
}
double ternary_search(double l, double r) {
double error = 2.0e-12;
while (r - l > error) {
double m1 = l + (r - l) / 3;
double m2 = r - (r - l) / 3;
double f1 = weakness(m1);
double f2 = weakness(m2);
if (f1 < f2)
r = m2;
else
l = m1;
}
return weakness(l);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
t = 1;
while (t--) {
cin >> n;
double mi = 2.0e6;
double ma = -2.0e6;
for (int i = 0; i < n; i++) {
cin >> a[i];
mi = min(a[i], mi);
ma = max(a[i], ma);
}
double ans = ternary_search(mi - 1, ma + 1);
ans *= 1.0e7;
long long s = trunc(ans);
ans = s / (1.0e7);
std::cout << setprecision(7) << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, a[200001];
const long double esl = 1e-15;
double cal(double x) {
long double t = 0;
long double ans = 0;
for (int i = 1; i <= n; i++) {
if (t + a[i] - x < 0) {
ans = max(ans, t);
t = 0;
} else {
t += ((long double)a[i] - x);
ans = max(ans, t);
}
}
ans = max(ans, t);
long double t1 = 0;
for (int i = 1; i <= n; i++) {
if (t1 + a[i] - x > 0) {
ans = max(ans, -t1);
t1 = 0;
} else {
t1 += ((long double)a[i] - x);
ans = max(ans, -t1);
}
}
ans = max(ans, -t1);
return ans;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
long double l = -(1e9) - 1;
long double r = 1e9 + 1;
while (abs(r - l) >= esl) {
long double d = (2 * l + r) / 3;
long double c = (2 * r + l) / 3;
if (cal(d) > cal(c)) {
l = d;
} else
r = c;
}
cout.precision(20);
cout << cal(l);
return 0;
}
|
#include <bits/stdc++.h>
int arr[200000];
int main() {
int n;
double lp = -10000;
double rp = 10000;
double med, a, b, c, d;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", arr + i);
}
while (rp - lp > 1e-12) {
med = (lp + rp) / 2;
a = -10001;
b = 10001;
c = d = 0;
for (int i = 0; i < n; ++i) {
c += arr[i] - med;
if (c < 0) c = 0;
if (c > a) a = c;
d += arr[i] - med;
if (d > 0) d = 0;
if (d < b) b = d;
}
if (a + b > 0) {
lp = med;
} else {
rp = med;
}
}
printf("%.9f\n", a);
}
|
#include <bits/stdc++.h>
using namespace std;
void Read(int& x, bool mark = 0) {
char tt = getchar();
for (; tt < '0' || '9' < tt; tt = getchar())
if (tt == '-') mark = 1;
for (x = 0; '0' <= tt && tt <= '9';
x = (x << 1) + (x << 3) + tt - '0', tt = getchar())
;
x = mark ? -x : x;
}
int n;
double a[200010];
double Left = 1e10;
double Right = -1e10;
void Init() {
Read(n);
for (int i = (1), c = (n); i <= c; ++i) {
scanf("%lf", a + i);
Left = min(Left, a[i]);
Right = max(Right, a[i]);
}
}
double Cal(double x) {
double prefix = 0;
double minSofar = 1e10;
double maxSofar = -1e10;
for (int i = (1), c = (n); i <= c; ++i) {
prefix += a[i] - x;
minSofar = min(minSofar, prefix);
maxSofar = max(maxSofar, prefix);
}
if (minSofar * maxSofar > 0)
return max(fabs(minSofar), fabs(maxSofar));
else
return maxSofar - minSofar;
}
void Solve() {
double Mid, Mmid;
for (int asd = (0), c = (100); asd <= c; ++asd) {
Mid = (2 * Left + Right) / 3;
Mmid = (Left + 2 * Right) / 3;
if (Cal(Mid) < Cal(Mmid)) {
Right = Mmid;
} else
Left = Mid;
}
printf("%.6lf\n", Cal(0.5 * (Left + Right)));
}
int main() {
Init();
Solve();
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.