text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; const int N = 210003; double a[N], b[N]; int n; double checkMa(double x) { for (int i = 1; i <= n; i++) b[i] = a[i] - x; double ans = 0, tpans = 0; for (int i = 1; i <= n; i++) { tpans += b[i]; if (tpans < 0) tpans = 0; ans = max(ans, tpans); } for (int i = 1; i <= n; i++) b[i] = -b[i]; tpans = 0; for (int i = 1; i <= n; i++) { tpans += b[i]; if (tpans < 0) tpans = 0; ans = max(ans, tpans); } return ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lf", &a[i]); } double r = 10005, l = -10005; int times = 100; while (fabs(r - l) > 1e-13 && times) { times--; if (times <= 0) break; double lm = (2.0 * l + r) / 3.0; double rm = (2.0 * r + l) / 3.0; if (checkMa(lm) < checkMa(rm)) { r = rm; } else l = lm; } printf("%.10f\n", checkMa(l)); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, cnt; double x[200010], inf = 1e9, eps = 1e-12; double cal(double mi) { int i, s; double s1, s2, mx, mn; s1 = s2 = 0; mx = -inf; mn = inf; for (i = 1; i <= n; i++) { if (s1 + x[i] - mi < 0) s1 = 0; else s1 += x[i] - mi; if (s2 + x[i] - mi > 0) s2 = 0; else s2 += x[i] - mi; mx = max(mx, s1); mn = min(mn, s2); } return max(abs(mx), abs(mn)); } int main() { int i, s, a, b, j, k; double l, r, m1, m2; while (cin >> n) { for (i = 1; i <= n; i++) scanf("%lf", &x[i]); l = -inf; r = inf; for (i = 0; i <= 600; i++) { m1 = (l + l + r) / 3.0; m2 = (l + r + r) / 3.0; if (cal(m1) < cal(m2)) r = m2; else l = m1; } printf("%.10lf\n", cal(l)); } return 0; }
#include <bits/stdc++.h> using namespace std; int a[200005], n; double judge(double x) { double ret = 0, sum = 0; for (int i = 0; i < n; ++i) { sum += a[i] - x; if (sum < 0) sum = 0; else { ret = max(ret, sum); } } sum = 0; for (int i = 0; i < n; ++i) { sum += x - a[i]; if (sum < 0) sum = 0; else { ret = max(ret, sum); } } return ret; } int main() { double l = 10000, r = -10000, mid, mid1, mid2, t, t1, t2; cin >> n; for (int i = 0; i < n; ++i) { scanf("%d", a + i); l = min(l, 1.0 * a[i]); r = max(r, 1.0 * a[i]); } double p, q; while (r - l >= 2e-12) { p = (2 * l + r) / 3; q = (2 * r + l) / 3; t1 = judge(p); t2 = judge(q); if (t1 < t2) r = q; else l = p; } printf("%.7lf", t1); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; double a[MAXN], b[MAXN]; int n; void input(); double f(double); void solve(); int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); input(); solve(); return 0; } void input() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; } double f(double x) { double res, mn, mx; res = abs(a[0] - x); b[0] = mn = mx = a[0] - x; mx = max(0.0, mx); mn = min(0.0, mn); for (int i = 1; i < n; i++) { b[i] = b[i - 1] + a[i] - x; res = max(res, max(abs(b[i] - mn), abs(mx - b[i]))); mn = min(mn, b[i]); mx = max(mx, b[i]); } return res; } void solve() { double l = -1e4 - 1, r = 1e4 + 1, mid = (l + r) / 2, eps = 1e-12; for (int i = 0; i < 100; i++) { (f(mid) < f(mid + eps)) ? r = mid : l = mid; mid = (l + r) / 2; } cout.precision(15); cout << fixed << f(l); }
#include <bits/stdc++.h> using namespace std; const int MAX = 2e5 + 9; int n; double a[MAX], sum[MAX]; double cal(double X) { double hsgs = 0, Mn = 0, Mx = 0; for (int i = 1; i <= n; i++) { sum[i] = sum[i - 1] + a[i] - X; hsgs = max(max(fabs(sum[i] - Mn), fabs(sum[i] - Mx)), hsgs); Mn = min(Mn, sum[i]), Mx = max(Mx, sum[i]); } return hsgs; } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; double l = -1e9 - 9, r = 1e9 + 9; for (int i = 1; i <= 250; i++) if (cal((2 * l + r) / 3) < cal((l + 2 * r) / 3)) r = (l + 2 * r) / 3; else l = (2 * l + r) / 3; cout << fixed << setprecision(10) << cal(l); }
#include <bits/stdc++.h> using namespace std; const double INF = 1e10; const int IT = 200; const int N = 200100; int n; double a[N]; double b[N]; double calcMax() { double res = 0; double bal = 0; for (int i = 0; i < n; i++) { res = max(res, bal); bal += b[i]; bal = max(bal, 0.); } return max(res, bal); } double calcMin() { double res = 0; double bal = 0; for (int i = 0; i < n; i++) { res = min(res, bal); bal += b[i]; bal = min(bal, 0.); } return min(res, bal); } bool check(double x) { for (int i = 0; i < n; i++) b[i] = a[i] - x; double A = calcMax(); double B = calcMin(); return (-B > A); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lf", &a[i]); double L = -INF, R = INF; for (int it = 0; it < IT; it++) { double x = (L + R) / 2; if (check(x)) R = x; else L = x; } for (int i = 0; i < n; i++) b[i] = a[i] - L; printf("%.12lf\n", calcMax()); return 0; }
#include <bits/stdc++.h> using namespace std; int IntMaxVal = (int)1e20; int IntMinVal = (int)-1e20; long long LongMaxVal = (long long)1e20; long long LongMinVal = (long long)-1e20; template <typename T> inline void minimize(T& a, T b) { a = std::min(a, b); } template <typename T> inline void maximize(T& a, T b) { a = std::max(a, b); } template <typename T> inline void swap(pair<T, T>& p) { swap(p.first, p.second); } template <typename T> struct argument_type; template <typename T, typename U> struct argument_type<T(U)> {}; template <typename T1, typename T2> istream& operator>>(istream& is, pair<T1, T2>& s) { is >> s.first >> s.second; return is; } template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { for (int i = 0; i < v.size(); i++) { if (i) os << ' '; os << v[i]; } os << '\n'; return os; } template <typename T1, typename T2> ostream& operator<<(ostream& os, const vector<pair<T1, T2>>& v) { for (int i = 0; i < v.size(); i++) { os << v[i] << '\n'; } return os; } template <typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& t) { s << t.first << ' ' << t.second; return s; } template <typename T> vector<T> readVector(int n) { vector<T> res(n); for (int i = 0; i < n; i++) scanf("%d", &res[i]); return res; } vector<double> vv; double get_max_sum(int l, int r) { if (l == r) return max(0.0, vv[l]); double ans = 0; int mid = (l + r) / 2; maximize(ans, get_max_sum(l, mid)); maximize(ans, get_max_sum(mid + 1, r)); double max_prefix = 0; double cur_prefix = 0; for (int i = mid; i >= l; --i) { cur_prefix += vv[i]; maximize(max_prefix, cur_prefix); } double max_suffix = 0; double cur_suffix = 0; for (int i = mid + 1; i < r + 1; ++i) { cur_suffix += vv[i]; maximize(max_suffix, cur_suffix); } maximize(ans, max_prefix + max_suffix); return ans; } double check(vector<int>& v2, double x, double& a) { int n = v2.size(); for (int i = 0; i < n; ++i) vv[i] = v2[i] - x; double a1 = fabs(get_max_sum(0, n - 1)); for (int i = 0; i < n; ++i) vv[i] = -vv[i]; double a2 = fabs(get_max_sum(0, n - 1)); a = max(a1, a2); return a1 > a2; } double get_a(vector<int>& v2, double x) { int n = v2.size(); for (int i = 0; i < n; ++i) vv[i] = v2[i] - x; double ans = 0; maximize(ans, fabs(get_max_sum(0, n - 1))); for (int i = 0; i < n; ++i) vv[i] = -vv[i]; maximize(ans, fabs(get_max_sum(0, n - 1))); return ans; } int main() { srand(time(NULL)); ios_base::sync_with_stdio(false); cin.tie(NULL); fixed(cout); cout << setprecision(10); int n; scanf("%d", &n); auto v = readVector<int>(n); vv.resize(n); double xl = -1e9; double xr = 1e9; double al = get_a(v, xl); double ar = get_a(v, xr); for (int it = 0; it < 100; ++it) { double mid = (xr + xl) / 2; double a; if (check(v, mid, a)) { xl = mid; al = a; } else { xr = mid; ar = a; } } cout << (al + ar) / 2; }
#include <bits/stdc++.h> using namespace std; int n, a[200000 + 1]; double x[200000 + 1]; double dp1[200000 + 1], dp2[200000 + 1], dp[200000 + 1]; double f(double delta) { for (int i = 0; i < n; i++) x[i] = a[i] - delta; dp1[0] = x[0]; for (int i = 1; i < n; i++) { dp1[i] = max(dp1[i - 1] + x[i], x[i]); } dp2[0] = x[0]; for (int i = 1; i < n; i++) { dp2[i] = min(dp2[i - 1] + x[i], x[i]); } dp[0] = abs(x[0]); for (int i = 1; i < n; i++) { dp[i] = abs(x[i]); dp[i] = max(abs(dp1[i - 1] + x[i]), dp[i]); dp[i] = max(abs(dp2[i - 1] + x[i]), dp[i]); } double ans = dp[0]; for (int i = 1; i < n; i++) ans = max(ans, dp[i]); return ans; } int main() { while (cin >> n) { for (int i = 0; i < n; i++) scanf("%d", &a[i]); double lo = -10001, hi = 10001; for (int i = 0; i < 100; i++) { double m1 = lo + 1.0 * (hi - lo) / 3; double m2 = lo + 2.0 * (hi - lo) / 3; double x1 = f(m1), x2 = f(m2); if (x1 > x2) lo = m1; else hi = m2; } printf("%.10lf\n", f((lo + hi) / 2.0)); } return 0; }
#include <bits/stdc++.h> using namespace std; double calc_weakness(const vector<int> nums, double x) { double min_sum = 0, max_sum = 0; double biggest_abs = 0; for (int i = nums.size(); i-- > 0;) { min_sum = std::min(nums[i] - x, nums[i] - x + min_sum); max_sum = std::max(nums[i] - x, nums[i] - x + max_sum); biggest_abs = std::max(biggest_abs, std::max(abs(min_sum), abs(max_sum))); } return biggest_abs; } void solve() { int n; cin >> n; vector<int> a(n); for (int(i) = 0; (i) < (n); (i)++) cin >> a[i]; double low = -20000; double high = 20000; while (true) { double step = (high - low) / 3; double p1 = low + step; double p2 = p1 + step; double v0 = calc_weakness(a, low); double v1 = calc_weakness(a, p1); double v2 = calc_weakness(a, p2); double v3 = calc_weakness(a, high); if (v3 - v2 < 1e-7 || (v3 - v2) / max(abs(v3), abs(v2)) < 1e-7) { double ans = (v3 + v2) / 2; cout.precision(30); cout << fixed << ans << endl; return; } if (v1 > v0) { high = p1; continue; } if (v2 > v1) { high = p2; continue; } low = p1; } } int main() { ios_base::sync_with_stdio(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int A[200000]; int main() { int N; scanf("%d", &N); for (int i = 0; i < N; i++) scanf("%d", &A[i]); long double mx, mn, l = -10000, r = 10000, m; while (r - l > 1e-13) { m = (l + r) / 2; mx = -1e20, mn = 1e20; long double tx = 0, tn = 0; for (int i = 0; i < N; i++) { mx = max(mx, tx = A[i] - m + max(0.l, tx)); mn = min(mn, tn = A[i] - m + min(0.l, tn)); } mx + mn > 0 ? l = m : r = m; } printf("%.8f", (double)mx); return 0; }
#include <bits/stdc++.h> using namespace std; const int MX = 201020; int N; int ip[MX]; double ans = 1e9; bool test(double x) { double minAns, minCur, maxAns, maxCur; minAns = maxAns = minCur = maxCur = 0.0; for (int i = 0; i < N; i++) { double t = ip[i] - x; minCur += t; maxCur += t; minCur = min(minCur, 0.0); maxCur = max(maxCur, 0.0); minAns = min(minAns, minCur); maxAns = max(maxAns, maxCur); } ans = min(ans, max(maxAns, -minAns)); return maxAns >= (-minAns); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N; for (int i = 0; i < N; i++) { cin >> ip[i]; } double l = -10000, r = 10000; while (l < r - 1e-11) { double md = (l + r) / 2.0; if (test(md)) { l = md; } else { r = md; } } test((l + r) / 2); cout << fixed << setprecision(10) << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void RI() {} template <typename... T> void RI(int &head, T &...tail) { scanf("%d", &head); RI(tail...); } const int N = 2e5 + 10; double mcs(int n, double a[N]) { double mx = 0, now = 0; for (int i = 0; i < int(n); i++) { now += a[i]; if (now > mx) mx = now; if (now < 0) now = 0; } return mx; } int n, a[N]; double b[N]; void calc(double m, double &vp, double &vn) { for (int i = 0; i < int(n); i++) b[i] = a[i] - m; vp = mcs(n, b); for (int i = 0; i < int(n); i++) b[i] *= -1; vn = mcs(n, b); } int main() { RI(n); for (int i = 0; i < int(n); i++) RI(a[i]); double l = *min_element(a, a + n); double r = *max_element(a, a + n); for (int t = 0; t < int(100); t++) { double m = (l + r) / 2, vp, vn; calc(m, vp, vn); if (vp > vn) l = m; else r = m; } double m = (l + r) / 2, vp, vn; calc(m, vp, vn); printf("%.12f\n", max(vp, vn)); 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]); a[i] += 10000; 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; const int MOD = 1e9 + 7; const int maxn = 300009; const int maxm = 300009; const double pi = acos(-1.0); const double eps = 1e-15; double data[maxn], sum, maxx, minx; double tmp[maxn]; double f2(double x, int n) { for (int i = 1; i <= n; i++) tmp[i] = data[i] - x; double tmpp = tmp[1], tmpn = tmp[1]; double ans = tmpp, ans2 = tmpn; for (int i = 2; i <= n; i++) { if (tmpp > 0) tmpp += tmp[i]; else tmpp = tmp[i]; ans = max(tmpp, ans); if (tmpn < 0) tmpn += tmp[i]; else tmpn = tmp[i]; ans2 = min(tmpn, ans2); } return max(abs(ans), abs(ans2)); } double sanfen(int n) { double l = minx, r = maxx, mid = l + (r - l) / 3., midd = r - (r - l) / 3.; int cnt = 100; while (fabs(r - l) > eps && cnt--) { double fmid = f2(mid, n), fmidd = f2(midd, n); if (fmid < fmidd) r = midd; else l = mid; mid = l + (r - l) / 3., midd = r - (r - l) / 3.; } return f2((l + r) / 2., n); } int main() { int n; while (~scanf("%d", &n)) { maxx = -1000000.; minx = 1000000.; sum = 0.0; for (int i = 1; i <= n; i++) scanf("%lf", &data[i]), sum += data[i]; if (n != 1) printf("%.15f\n", sanfen(n)); else printf("%.15lf\n", 0.0); } return 0; }
#include <bits/stdc++.h> using namespace std; long long int modpow(long long int a, long long int n, long long int temp) { long long int res = 1, y = a; while (n > 0) { if (n & 1) res = (res * y) % temp; y = (y * y) % temp; n /= 2; } return res % temp; } vector<double> c; double mcs(vector<int> &a, double x) { int i; for (i = 0; i < c.size(); ++i) c[i] = a[i] - x; double sum = 0, res = 0; for (i = 0; i < c.size(); ++i) { sum += c[i]; res = max(res, sum); if (sum < 0) sum = 0; } return res; } int main() { int n, i; cin >> n; c = vector<double>(n); vector<int> a(n), b(n); for (i = 0; i < n; ++i) { cin >> a[i]; b[i] = -a[i]; } double l = *min_element(a.begin(), a.end()), r = *max_element(a.begin(), a.end()); while (fabs(l - r) > 1e-11) { double m = (l + r) / 2; double pos = mcs(a, m); double neg = mcs(b, -m); if (pos > neg) { l = m; } else { r = m; } } double m = (l + r) / 2; printf("%.7lf", max(mcs(a, m), mcs(b, -m))); return 0; }
#include <bits/stdc++.h> using namespace std; double a[200006]; int n; double kadane(int flag, double x) { double res = (-1.0) * 1e18; double temp = 0.0; for (int i = 1; i <= n; i++) { if (flag) temp += x - a[i]; else temp += a[i] - x; res = max(res, temp); temp = max(0.0, temp); } return res; } double cal(double x) { double res1, res2; res1 = kadane(0, x); res2 = kadane(1, x); return max(res1, res2); } double ternary_search(double l, double r) { double eps = 200; while (eps--) { double m1 = l + (r - l) / 3.0; double m2 = r - (r - l) / 3.0; double f1 = cal(m1); double f2 = cal(m2); if (f1 > f2) l = m1; else r = m2; } return cal(l); } int main(int argc, char const *argv[]) { double mn, mx; cin >> n; for (int i = 1; i <= n; i++) { scanf("%lf", &a[i]); } printf("%.10lf\n", ternary_search(-10000.0, 10000.0)); return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-11; vector<double> a; double okk(double m) { long long n = a.size(); double ans = 0; double tmp[n], pf[n]; for (long long i = 0; i < n; i++) { tmp[i] = a[i] - m; if (i == 0) pf[i] = tmp[i]; else pf[i] = pf[i - 1] + tmp[i]; } double mn = 0; for (long long i = 0; i < n; i++) { ans = max(ans, pf[i] - mn); mn = min(mn, pf[i]); pf[i] *= -1; } mn = 0; for (long long i = 0; i < n; i++) { ans = max(ans, pf[i] - mn); mn = min(mn, pf[i]); } return ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << setprecision(15) << fixed; long long n; cin >> n; a.resize(n); for (long long i = 0; i < n; i++) { cin >> a[i]; } double l = -1e6, r = 1e6, ans = 1e15; while (r - l > eps) { double m1 = l + (r - l) / 3; double m2 = r - (r - l) / 3; double t1 = okk(m1); double t2 = okk(m2); ans = min(ans, t1); ans = min(ans, t2); if (t1 > t2) l = m1; else r = m2; } cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; int a[200005]; long double b[200005]; int n; long double solve() { long double ans = 0; long double sum1 = 0, sum2 = 0; for (int i = 1; i <= n; i++) { sum1 = max(b[i], sum1 + b[i]); ans = max(ans, sum1); b[i] = -b[i]; sum2 = max(b[i], sum2 + b[i]); ans = max(ans, sum2); } return ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } long double lo = -1e9, hi = 1e9, ans = 1e9; for (int i = 0; i < 200; i++) { long double mid1 = lo + (hi - lo) / 3; long double mid2 = hi - (hi - lo) / 3; long double res1 = -1e18, res2 = -1e18; for (int i = 1; i <= n; i++) { b[i] = a[i] - mid1; } res1 = solve(); for (int i = 1; i <= n; i++) { b[i] = a[i] - mid2; } res2 = solve(); if (res1 <= res2) { ans = min(ans, res1); hi = mid2; } else { ans = min(ans, res2); lo = mid1; } } printf("%.20Lf\n", ans); }
#include <bits/stdc++.h> using namespace std; namespace integer_search { template <typename Datatype, typename Function> Datatype first_true(Datatype l, Datatype r, const Datatype not_found, const Function& f) { Datatype ans = not_found; while (l <= r) { const Datatype m = l + (r - l) / 2; if (f(m)) { ans = m; r = m - 1; } else { l = m + 1; } } return ans; } template <typename Datatype, typename Function> Datatype first_false(Datatype l, Datatype r, const Datatype not_found, const Function& f) { Datatype ans = not_found; while (l <= r) { const Datatype m = l + (r - l) / 2; if (!f(m)) { ans = m; r = m - 1; } else { l = m + 1; } } return ans; } template <typename Datatype, typename Function> Datatype last_true(Datatype l, Datatype r, const Datatype not_found, const Function& f) { Datatype ans = not_found; while (l <= r) { const Datatype m = l + (r - l) / 2; if (f(m)) { ans = m; l = m + 1; } else { r = m - 1; } } return ans; } template <typename Datatype, typename Function> Datatype last_false(Datatype l, Datatype r, const Datatype not_found, const Function& f) { Datatype ans = not_found; while (l <= r) { const Datatype m = l + (r - l) / 2; if (!f(m)) { ans = m; l = m + 1; } else { r = m - 1; } } return ans; } template <typename Datatype, typename Function> Datatype argmax(Datatype l, Datatype r, const Function& f) { Datatype ans = l; l += 1; while (l <= r) { const Datatype m = l + (r - l) / 2; if (f(m) > f(m - 1)) { ans = m; l = m + 1; } else { r = m - 1; } } return ans; } template <typename Datatype, typename Function> Datatype argmin(Datatype l, Datatype r, const Function& f) { Datatype ans = l; l += 1; while (l <= r) { const Datatype m = l + (r - l) / 2; if (f(m) < f(m - 1)) { ans = m; l = m + 1; } else { r = m - 1; } } return ans; } template <typename Datatype, typename Function> auto maximum(Datatype l, Datatype r, const Function& f) -> decltype(f(l)) { auto ans = f(l); l += 1; while (l <= r) { const Datatype m = l + (r - l) / 2; const auto fm = f(m); const auto fmm1 = f(m - 1); if (fm > fmm1) { ans = fm; l = m + 1; } else { r = m - 1; } } return ans; } template <typename Datatype, typename Function> auto minimum(Datatype l, Datatype r, const Function& f) -> decltype(f(l)) { auto ans = f(l); l += 1; while (l <= r) { const Datatype m = l + (r - l) / 2; const auto fm = f(m); const auto fmm1 = f(m - 1); if (fm < fmm1) { ans = fm; l = m + 1; } else { r = m - 1; } } return ans; } } // namespace integer_search namespace floating_search { template <typename Datatype, typename Function> Datatype first_true(Datatype l, Datatype r, const Datatype not_found, const int iterations, const Function& f) { Datatype ans = not_found; for (int iteration = 0; iteration < iterations; ++iteration) { const Datatype m = l + (r - l) / 2; if (f(m)) { ans = m; r = m; } else { l = m; } } return ans; } template <typename Datatype, typename Function> Datatype first_false(Datatype l, Datatype r, const Datatype not_found, const int iterations, const Function& f) { Datatype ans = not_found; for (int iteration = 0; iteration < iterations; ++iteration) { const Datatype m = l + (r - l) / 2; if (!f(m)) { ans = m; r = m; } else { l = m; } } return ans; } template <typename Datatype, typename Function> Datatype last_true(Datatype l, Datatype r, const Datatype not_found, const int iterations, const Function& f) { Datatype ans = not_found; for (int iteration = 0; iteration < iterations; ++iteration) { const Datatype m = l + (r - l) / 2; if (f(m)) { ans = m; l = m; } else { r = m; } } return ans; } template <typename Datatype, typename Function> Datatype last_false(Datatype l, Datatype r, const Datatype not_found, const int iterations, const Function& f) { Datatype ans = not_found; for (int iteration = 0; iteration < iterations; ++iteration) { const Datatype m = l + (r - l) / 2; if (!f(m)) { ans = m; l = m; } else { r = m; } } return ans; } template <typename Datatype, typename Function> Datatype argmax(Datatype l, Datatype r, const int iterations, const Function& f) { const Datatype golden_ratio = (1 + std::sqrt((Datatype)5)) / 2; Datatype m1 = r - (r - l) / golden_ratio; Datatype m2 = l + (r - l) / golden_ratio; auto fm1 = f(m1); auto fm2 = f(m2); for (int iteration = 0; iteration < iterations; ++iteration) { if (fm1 > fm2) { r = m2; m2 = m1; fm2 = fm1; m1 = r - (r - l) / golden_ratio; fm1 = f(m1); } else { l = m1; m1 = m2; fm1 = fm2; m2 = l + (r - l) / golden_ratio; fm2 = f(m2); } } return (l + r) / 2; } } // namespace floating_search int32_t main() { int n; cin >> n; int v[n]; for (int i = 0; i < n; ++i) cin >> v[i]; auto f = [&](const double x) { double sum1 = 0, sum2 = 0; double best = 0; for (int i = 0; i < n; ++i) { double val = v[i] - x; sum1 += val; if (sum1 < 0) sum1 = 0; sum2 -= val; if (sum2 < 0) sum2 = 0; best = max(best, sum1); best = max(best, sum2); } return -best; }; double arg = floating_search::argmax(-1e4, 1e4, 500, f); double ans = f(arg); cout << fixed << setprecision(12) << -ans << endl; }
#include <bits/stdc++.h> using namespace std; int n; int a[200009]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } double l, r, mid; double maxpos, maxneg, tmpp, tmpn, ta; int i; l = -100001; r = 100001; int deepness = 0; while (fabs(r - l) > 0.0000000000001) { deepness++; mid = (l + r) / 2.0; maxpos = 0.000000000000; maxneg = 0.000000000000; tmpp = 0.000000000000; tmpn = 0.000000000000; for (i = 1; i <= n; i++) { ta = a[i] - mid; tmpp += ta; tmpn += -ta; maxpos = max(tmpp, maxpos); maxneg = max(tmpn, maxneg); tmpp = max(tmpp, 0.0000000000); tmpn = max(tmpn, 0.0000000000); } if (maxpos > maxneg) { l = mid; } else { r = mid; } if (deepness >= 100) break; } printf("%.10lf", max(maxpos, maxneg)); }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const double oo = 1e5 + 1; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; vector<double> a(n); for (long long i = 0; i < n; i++) { cin >> a[i]; } auto f = [&](double x) { double Max = 0, Min = 0, cur = 0, ans = 0; for (long long i = 0; i < n; i++) { cur += a[i] - x; ans = max(ans, abs(cur - Min)); ans = max(ans, abs(cur - Max)); Min = min(Min, cur); Max = max(Max, cur); } return ans; }; double l = -1e4, r = 1e4; for (long long i = 1; i <= 300; i++) { double mid1 = l + (r - l) / 3; double mid2 = r - (r - l) / 3; if (f(mid1) < f(mid2)) r = mid2; else l = mid1; } cout << fixed << setprecision(10) << f(l); }
#include <bits/stdc++.h> int n; double a[200005]; double f(double x) { int i; double b = 0, c = 0, d; for (i = 0; i < n; i++) { b += (a[i] - x); if (b < 0) b = 0; if (b >= c) c = b; } b = 0; for (i = 0; i < n; i++) { b += (x - a[i]); if (b < 0) b = 0; if (b >= c) c = b; } return c; } int main() { int i, j = 100; double m1, m2, v1, v2, l = -10000.0, r = 10000.0; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%lf", &a[i]); while (j--) { m1 = l + (r - l) / 3.0; m2 = r - (r - l) / 3.0; v1 = f(m1); v2 = f(m2); if (v1 > v2) l = m1; else r = m2; } printf("%.7f00000000\n", f(l)); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; const int it_count = 150; int a[maxn]; int n; inline double f(double x) { double cur = 0, minn = 0, maxx = 0; double ret = 0.0; for (int pos = 0; pos < (int)n; pos++) { cur += a[pos] - x; minn = min(minn, cur); maxx = max(maxx, cur); ret = max(ret, max(abs(cur - minn), abs(cur - maxx))); } return ret; } int main() { scanf("%d", &n); for (int j = 0; j < (int)n; j++) scanf("%d", &a[j]); double start = -2e4; double finish = 2e4; for (int iter = 0; iter < (int)it_count; iter++) { double m1 = start + (finish - start) / 3; double m2 = finish - (finish - start) / 3; double f1 = f(m1); double f2 = f(m2); if (f1 < f2) finish = m2; else start = m1; } double ans = f(start); printf("%.10lf\n", ans); }
#include <bits/stdc++.h> using namespace std; long long a[222222]; long long n; double f(double x) { double s = 0, mx = 0, mn = 0; for (int i = 1; i <= n; i++) { s += a[i] - x; if (s < 0) s = 0; if (s > mx) mx = s; } s = 0; for (int i = 1; i <= n; i++) { s += a[i] - x; if (s > 0) s = 0; if (s < mn) mn = s; } mn = -mn; return mx > mn ? mx : mn; } int main() { scanf("%lld", &n); for (int i = 1; i <= n; i++) scanf("%lld", a + i); long double l = -20000, r = 20000, eps = 1e-9; int t = 300; while (t--) { long double m1 = (l + l + r) / 3; long double m2 = (r + r + l) / 3; long double f1 = f(m1); long double f2 = f(m2); if (f1 >= f2) l = m1; else r = m2; } printf("%.10f", f(l)); }
#include <bits/stdc++.h> using namespace std; template <class C> constexpr int sz(const C &c) { return int(c.size()); } using ll = long long; using ld = long double; constexpr const char nl = '\n', sp = ' '; template <class T, class F, class Cmp = less<T>> T tsearch(T lo, T hi, F f, int iters, Cmp cmp = less<T>()) { for (int it = 0; it < iters; it++) { T m1 = lo + (hi - lo) / 3, m2 = hi - (hi - lo) / 3; if (cmp(f(m1), f(m2))) lo = m1; else hi = m2; } return lo + (hi - lo) / 2; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int N; cin >> N; vector<ld> A(N); for (auto &&a : A) cin >> a; auto f = [&](ld x) { ld ret = 0, cur = 0; for (auto &&a : A) { cur += a - x; if (cur < 0) cur = 0; if (ret < cur) ret = cur; } cur = 0; for (auto &&a : A) { cur -= a - x; if (cur < 0) cur = 0; if (ret < cur) ret = cur; } return ret; }; cout << fixed << setprecision(9) << f(tsearch(ld(-3e4), ld(3e4), f, 300, greater<ld>())) << nl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 200000, MAX = 10000; double A[N]; int n; double calc() { double ret = 0, cur = 0; for (int i = 0; i < n; i++) { cur = max(cur + A[i], 0.0); ret = max(ret, cur); } return ret; } double f(double delta) { for (int i = 0; i < n; i++) A[i] += delta; double ret = calc(); for (int i = 0; i < n; i++) A[i] *= -1; ret = max(ret, calc()); for (int i = 0; i < n; i++) A[i] *= -1; for (int i = 0; i < n; i++) A[i] -= delta; return ret; } double search(double from, double to) { for (int i = 0; i < 100; i++) { double m1 = (2 * from + to) / 3, m2 = (from + 2 * to) / 3; if (f(m1) < f(m2)) to = m2; else from = m1; } return from; } bool solve(int testNumber) { if (scanf("%d", &n) == EOF) return false; for (int i = 0; i < n; i++) scanf("%lf", &A[i]); printf("%.12f\n", f(search(-MAX, MAX))); return true; } int main() { for (int i = 1; solve(i); i++) ; return 0; }
#include <bits/stdc++.h> using namespace std; int n, a[200010]; double f(double x) { double maxm = 0, minm = 0, c = 0; for (int i = (0); i < (n); i++) c += a[i] - x, maxm = max(maxm, c), minm = min(minm, c); return maxm - minm; } int main() { cin >> n; for (int i = (0); i < (n); i++) cin >> a[i]; double l = -1e9, m1, m2, r = 1e9; for (int i = (0); i < (200); i++) { m1 = l + (r - l) / 3; m2 = r - (r - l) / 3; if (f(m1) < f(m2)) r = m2; else l = m1; } printf("%lf\n", f(m1)); return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> void rd(T& t) { char c; t = 0; int k = 1; while ((c = getchar()) < '-') ; if (c == '-') k = -1, (c = getchar()); do t = t * 10 + (c ^ 48); while ((c = getchar()) > '/'); t *= k; } template <class T> void prinf(T t) { if (!t) return; prinf(t / 10); putchar(t % 10 ^ 48); } template <class T> void ptn(T t) { if (!t) putchar('0'); if (t < 0) putchar('-'), t = -t; prinf(t); } const int maxn = 2e5 + 5; int n, a[maxn]; double mx, mi; void solve(double x) { double sum = 0; for (int i = (1); i <= (n); i++) { sum += a[i] - x; mx = max(mx, sum); if (sum < 0) sum = 0; } sum = 0; for (int i = (1); i <= (n); i++) { sum += a[i] - x; mi = min(mi, sum); if (sum > 0) sum = 0; } } int main() { rd(n); for (int i = (1); i <= (n); i++) rd(a[i]); double l = -10001, r = 10001; for (int ii = (0); ii <= (100); ii++) { double mid = (l + r) / 2; mx = 0; mi = 0; solve(mid); if (mx > -mi) l = mid; else r = mid; } solve(l); printf("%lf", max(mx, -mi)); return 0; }
#include <bits/stdc++.h> using namespace std; int n; double a[200000]; double b[200000]; double kadane() { double ans = b[0]; double con = b[0]; for (int i = 1; i < n; i++) { con = max(con + b[i], b[i]); ans = max(ans, con); } return ans; } double f(double x) { for (int i = 0; i < n; i++) { b[i] = a[i] - x; } double ans = abs(kadane()); for (int i = 0; i < n; i++) { b[i] = -b[i]; } ans = max(ans, abs(kadane())); return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } double L = -10000; double R = 10000; for (int i = 0; i < 100; i++) { double M1 = (2 * L + R) / 3; double M2 = (L + 2 * R) / 3; f(M1) < f(M2) ? R = M2 : L = M1; } cout << fixed << setprecision(7) << min(f(L), f(R)) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int i, j, k, n, m, x, y, T, ans, big, cas, num, len; bool flag; double a[200005]; double chk(double x) { double pre, pre2, ans, ans2; ans = 0; ans2 = 0; pre = 0; pre2 = 0; for (int i = 1; i <= n; i++) { pre += a[i] - x; ans = max(ans, fabs(pre)); pre = max(pre, 0.0); pre2 += a[i] - x; ans2 = max(ans2, fabs(pre2)); pre2 = min(pre2, 0.0); } return max(fabs(ans), fabs(ans2)); } int main() { scanf("%d", &n); double mx = -0x3fffffff, mi = 0x3fffffff; for (i = 1; i <= n; i++) { scanf("%lf", &a[i]); mx = max(mx, a[i]); mi = min(mi, a[i]); } double l = mi, r = mx; while (true) { double m1 = (r + 2 * l) / 3; double m2 = (2 * r + l) / 3; double c1 = chk(m1), c2 = chk(m2); if (fabs(l - r) < 1e-11 && fabs(c1 - c2) < 1e-7) break; if (c1 < c2) r = (2 * r + l) / 3; else l = (r + 2 * l) / 3; } printf("%.12lf\n", chk(l)); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int m = 0, a = 0; for (int i = 0; i < 100; i++) { string s; getline(cin, s); if (s[0] == '+') { m++; continue; } if (s[0] == '-') { m--; continue; } int c = 1; int t = 0; for (int j = 0; j < s.size(); j++) { if (s[j] == ':' && c == 1) { c = 0; continue; } if (c == 0) { a += (s.size() - j) * m; break; } } } cout << a; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char newMess[501]; int countText = 0; int countPeople = 0; while (cin.get(newMess, 501, '\n')) { if (newMess[0] == '\n' && newMess[1] == '\n') break; else if (newMess[0] == '+') countPeople++; else if (newMess[0] == '-') countPeople--; else { int i = 0; while (newMess[i] != ':') i++; countText += countPeople * (strlen(newMess) - i - 1); } cin.get(); } cout << countText; }
#include <bits/stdc++.h> int main() { int k = 0; int total = 0; for (;;) { char buf[1000]; if (!gets(buf)) break; if (buf[0] == '+') k++; else if (buf[0] == '-') k--; else { int pos = -1; for (int i = 0; i < strlen(buf); i++) { if (buf[i] == ':') { pos = i; break; } } int len = strlen(buf) - pos - 1; total += len * k; } } printf("%d\n", total); return 0; }
#include <bits/stdc++.h> using namespace std; const int oo = INT_MAX; int main() { string s; set<string> myset; int sum = 0; while (getline(cin, s)) { if (s[0] == '+') s.erase(0, 1), myset.insert(s); else if (s[0] == '-') s.erase(0, 1), myset.erase(myset.find(s)); else sum += (s.size() - s.find(':') - 1) * myset.size(); } cout << sum << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int peo = 0, ans = 0; string s; while (getline(cin, s)) { if (s[0] == '+') { peo++; } else if (s[0] == '-') { peo--; } else { int add = s.length(); for (int i = 0; i < s.length(); i++) { add--; if (s[i] == ':') { break; } } ans += peo * add; } } cout << ans + 1 - 1; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; } set<string> names; char command[110]; int n; int ans = 0; void processAdd() { names.insert(command + 1); } void processRemove() { names.erase(names.find(command + 1)); } void processSend() { int lenMsg = n; for (int(i) = 0; (i) < (int)(n); ++(i)) { lenMsg--; if (command[i] == ':') break; } ans += lenMsg * names.size(); } void processCommand() { if (command[0] == '+') processAdd(); else if (command[0] == '-') processRemove(); else processSend(); } int main() { while (gets(command)) { n = strlen(command); processCommand(); } printf("%d\n", ans); }
#include <bits/stdc++.h> using namespace std; set<string> S; char buf[1000]; int main() { int ret = 0; while (true) { if (!(cin.getline(buf, 999))) break; if (buf[0] == '+') { string tec(buf + 1); S.insert(tec); } else if (buf[0] == '-') { string tec(buf + 1); S.erase(tec); } else { int tec = 0; while (buf[tec] != ':') tec++; ret += strlen(buf + tec + 1) * S.size(); } } cout << ret << endl; }
#include <bits/stdc++.h> using namespace std; int main() { string s; int n; int k = 0; int kk = 0; int kkk = 0; int count = 0; while (getline(cin, s)) { if (s[0] == '+') count++; else if (s[0] == '-') count--; else { for (int i = 0; i < s.length(); i++) { if (s[i] == ':') k++; if (k > 0 && s[i] != ':') kk++; } } kkk = kkk + kk * count; kk = 0; k = 0; } cout << kkk << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x7f7f7f7f; const double EPS = 1e-8; const double PI = acos(-1.0); const int dx[] = {0, -1, 0, 1}; const int dy[] = {1, 0, -1, 0}; const int fx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; const int fy[] = {-1, 0, 1, -1, 1, -1, 0, 1}; void openfile() { freopen("data.in", "rb", stdin); freopen("data.out", "wb", stdout); } const int N = 0; int cur; map<string, int> m; string s; int main() { long long ans = 0; while (getline(cin, s)) { if (s[0] == '+') { m[s.substr(1)]++; cur++; } else if (s[0] == '-') { m[s.substr(1)]--; cur--; } else { int pos = s.find(":"); ans += (s.length() - pos - 1) * cur; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> int main() { char str[101]; int num = 0, ans = 0, len; while (gets(str)) { if (str[0] == '+') num++; else if (str[0] == '-') num--; else { len = strlen(str) - 1; for (int i = 0; str[i] != ':'; i++) len--; ans += num * len; } } printf("%d", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char ans[105]; int count = 0, sum = 0, p = 0, flag = 0; for (int i = 0; i < 105; i++) { cin.getline(ans, 104); if (strlen(ans) == 0) break; if (ans[0] == '+') count++; else if (ans[0] == '-') count--; else { p = 0; flag = 0; for (int i = 0; i < strlen(ans); i++) { if (ans[i] == ':') { flag = 1; } if (flag == 1) p++; } sum = sum + ((p - 1) * count); } } cout << sum; }
#include <bits/stdc++.h> using namespace std; const int MAXL = 109; char str[MAXL]; int j, len; int cnt, ans; int main() { cnt = ans = 0; while (gets(str)) { if (str[0] == '+') cnt++; else if (str[0] == '-') cnt--; else { for (j = 0; str[j] != ':'; j++) ; for (len = 0, j++; str[j] != '\0'; j++) len++; ans += len * cnt; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int saldo, sum, n, i; char line[1024]; int main() { saldo = sum = 0; while (fgets(line, 1024, stdin)) { line[strlen(line) - 1] = '\0'; if (line[0] == '+') saldo++; else if (line[0] == '-') saldo--; else { n = strlen(line); for (i = 0; i < n; i++) if (line[i] == ':') break; sum += saldo * strlen(line + i + 1); } } printf("%d\n", sum); return 0; }
#include <bits/stdc++.h> using namespace std; void CIN() { cin.tie(0); ios_base::sync_with_stdio(0); } int main() { set<string> v; string s1; long long out = 0, i = 0; while (getline(cin, s1)) { if (s1[0] == '+') { s1.erase(0, 1); v.insert(s1); } else if (s1[0] == '-') { s1.erase(0, 1); v.erase(s1); } else { while (s1[i] != ':') { i++; } s1.erase(0, i + 1); out = out + (v.size() * s1.size()); } i = 0; } cout << out; return 0; }
#include <bits/stdc++.h> using namespace std; char s[200]; set<string> lst; int ans; int main() { ios::sync_with_stdio(false); int x = 10; while (cin.getline(s, 110)) { string t = s; if (t.size() < 2) continue; if (t[0] == '+') lst.insert(t.substr(1, t.size() - 1)); else if (t[0] == '-') lst.erase(t.substr(1, t.size() - 1)); else { int c = 0; while (t[c] != ':') c++; ans += (t.size() - c - 1) * lst.size(); } } cout << ans; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const int inf = 2e9; const int mod = 1e9 + 7; const long long INF = 2e18; string s, t; set<string> q; int res; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); while (getline(cin, s)) { t = ""; if (s[0] == '+') { q.insert(s.substr(1)); } else if (s[0] == '-') { q.erase(s.substr(1)); } else { int i = 0; while (s[i] != ':') { ++i; } ++i; for (; i < (int)s.size(); ++i) { res += (int)q.size(); } } } cout << res; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char s[101]; int i = 0, num = 0; long long sum = 0; while (gets(s) != NULL) { if (s[0] == '+' || s[0] == '-') { if (s[0] == '+') num++; else num--; } else { int j; for (j = 0; j < strlen(s); j++) { if (s[j] == ':') break; } if (j != strlen(s)) sum += num * (strlen(s) - j - 1); } } cout << sum << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int dirx[] = {0, 0, 1, -1, 0}; int diry[] = {1, -1, 0, 0, 0}; void solve() { string s; int cnt = 0, sum = 0; while (getline(cin, s)) { if (s[0] == '+') cnt++; else if (s[0] == '-') cnt--; else { int l = s.length(); int idx; for (int i = 0; i < l; i++) { if (s[i] == ':') { idx = i + 1; break; } } sum += (l - idx) * cnt; } } cout << sum << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); }
#include <bits/stdc++.h> using namespace std; map<string, int> a; string s, s1; int res, len, i; int main() { while (getline(cin, s)) { s1 = s.substr(1, s.length()); if (s[0] == '+') a[s1]++; else if (s[0] == '-') a.erase(s1); else { len = s.length() - s.find(":") - 1; res += a.size() * len; } } cout << res << endl; }
#include <bits/stdc++.h> using namespace std; int main() { string line; int users(0), total(0); while (getline(cin, line)) { if (line[0] == '+') users++; else if (line[0] == '-') users--; else total += users * (line.size() - line.find(':') - 1); } cout << total << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int cnt = 0, ans = 0, len; char s[1005]; int main() { while (cin.getline(s, 1005)) { len = strlen(s); if (s[0] == '+') cnt++; else if (s[0] == '-') cnt--; else { for (int i = (0); i <= (len - 1); i++) { if (s[i] == ':') { ans += cnt * (len - i - 1); break; } } } } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; int trafic = 0; int k = 0; while (getline(cin, s)) { if (s[0] == '+') k++; else if (s[0] == '-') k--; else for (int i = 0; i < s.size(); i++) if (s[i] == ':') { trafic += (s.size() - i - 1) * k; break; } } cout << trafic; return 0; }
#include <bits/stdc++.h> using namespace std; set<string> st; int main() { ios::sync_with_stdio(false); string str; int res = 0; while (getline(cin, str)) { int ty; if (str[0] == '+') ty = 0; else if (str[0] == '-') ty = 1; else ty = 2; if (ty < 2) { str = str.substr(1); if (ty == 0) st.insert(str); else st.erase(str); } else { int loc = 0; while (str[loc] != ':') loc++; str = str.substr(loc + 1); res += (int)str.length() * st.size(); } } printf("%d\n", res); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; int p = 0, ans = 0; while (getline(cin, s)) { if (s[0] == '+') p++; else if (s[0] == '-') p--; else ans += p * (s.length() - s.find(':') - 1); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; vector<string> names; int sum = 0; int main() { string str; while (getline(cin, str)) { if (str[0] == '+') names.push_back(str); else if (str[0] == '-') { str[0] = '+'; for (int i = 0; i < names.size(); i++) if (names[i] == str) names.erase(names.begin() + i); } else { int size = str.size(), n; for (int i = 0; i < str.size(); i++) if (str[i] == ':') n = i; ++n; size -= n; int result = (names.size() * size); sum += result; } } cout << sum << endl; return 0; }
#include <bits/stdc++.h> using namespace std; char s[1005]; int len2(char *s) { int x, y; sscanf(s, "%*[^>]>%n%*[^\0]%n", &x, &y); return y - x; } int len3(char *s) { char str[256]; sscanf(s, "%[^=]", str); printf(str); cout << endl; } int len4(char *s) { char str[256]; sscanf(s, "%*[^=]=%s", str); printf(str); cout << endl; } int len(char *s) { int len = strlen(s); int ptr = 0; while ((s[ptr] != ':') && (ptr < len)) ptr++; return len - ptr - 1; } int main() { int res = 0; int p = 0; while (gets(s)) { if (s[0] == '+') p++; else if (s[0] == '-') p--; else res += p * len(s); } cout << res << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n = 0, ans = 0; string s; while (getline(cin, s)) { int k = 0; if (s[0] == '+') n++; if (s[0] == '-') n--; else for (int i = 0; i < s.size(); i++) if (s[i] == ':') k = s.size() - i - 1; ans += k * n; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> int main() { char s1[1000], s2[1000]; int i, j, k, l, m, n, count; int total; count = 0; total = 0; while (1) { if (!gets(s1)) break; if (s1[0] == '+') ++count; else if (s1[0] == '-') --count; else { l = 0; for (i = 0; s1[i] != ':'; i++) ; for (i = i + 1; s1[i] != '\0'; i++) ++l; total = total + l * count; } } printf("%d\n", total); return 0; }
#include <bits/stdc++.h> using namespace std; string s; int cnt, sum; int main() { while (getline(cin, s)) { if (s[0] == '+') cnt++; else if (s[0] == '-') cnt--; else { int j; for (j = 0; j < s.size(); j++) if (s[j] == ':') break; sum += cnt * (s.size() - j - 1); } } cout << sum << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string str; int cnt = 0, res = 0; while (getline(cin, str)) { if (str[0] == '+') cnt++; else if (str[0] == '-') cnt--; else { int index = str.find(":"); int len = str.length() - 1 - index; res += (cnt * len); } } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char str[110]; int i, j = 0, n, num = 0, sum = 0; while (gets(str)) { if (str[0] == '+') num++; else if (str[0] == '-') num--; else { n = strlen(str); for (i = 0; i < n; i++) { if (str[i] == ':') { j = i; break; } } sum += (n - 1 - j) * num; } } printf("%d\n", sum); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string line; int ans = 0, cnt = 0; ; while (getline(cin, line)) { if (line[0] == '+') cnt++; else if (line[0] == '-') cnt--; else { int pos = 0; while (line[pos] != ':') ++pos; ans += cnt * (line.size() - pos - 1); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> char line[100000], message[100000], kind; int num, result; int main() { while (scanf("%[^\n]\n", line) != EOF) { char *now = line; while (now[0] != '+' && now[0] != '-' && now[0] != ':') now++; switch (now[0]) { case '+': num++; break; case '-': num--; break; case ':': result += (strlen(now) - 1) * num; break; } } printf("%d\n", result); return EXIT_SUCCESS; }
#include <bits/stdc++.h> using namespace std; int main() { int num = 0, c = 0, sum = 0; string s; while (getline(cin, s)) { if (s[0] == '+') num++; else if (s[0] == '-') num--; else { for (int i = 0; i < s.size(); i++) { if (s[i] == ':') { sum = s.size() - (i + 1); break; } } c += sum * num; } } cout << c << endl; return 0; }
#include <bits/stdc++.h> using namespace std; map<string, bool> mp; int command(string &s) { string tmp = ""; if (s[0] == '+') { for (int i = 1; i < s.size(); ++i) tmp += s[i]; mp[tmp] = 1; } else if (s[0] == '-') { for (int i = 1; i < s.size(); ++i) { tmp += s[i]; } auto del = mp.find(tmp); mp.erase(del); } else { int pos = 0; while (s[pos] != ':') ++pos; int num = s.size() - pos - 1; return (mp.size() * num); } return 0; } int main() { string s; long long ans = 0; while (getline(cin, s, '\n')) { ans += command(s); } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { string s; int a = 0, b = 0, c = 0, z = 0; while (getline(cin, s)) { if (s[0] == '+') a++; else if (s[0] == '-') a--; else { c += (s.size() - s.find(':') - 1) * a; } } cout << c; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s; long long n = 0, sum = 0; while (getline(cin, s)) { if (s[0] == '+') n++; else if (s[0] == '-') n--; else { for (int i = 0; i < s.size(); i++) { if (s[i] == ':') { sum += (n) * (s.size() - i - 1); break; } } } } cout << sum << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int add, ans; int main() { string s; while (getline(cin, s)) { if (s[0] == '+') add++; else if (s[0] == '-') add--; else ans += add * (s.size() - s.find(":") - 1); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { char aa[105][105]; char tmp[105]; int fig = 0; int ans = 0; int i = 0; while (gets(tmp) != 0) { if (tmp[0] == '+') fig++; else if (tmp[0] == '-') fig--; else { int num1 = 0; for (int i = 0; i < strlen(tmp); i++) { if (tmp[i] == ':') break; else num1++; } num1++; int num2 = strlen(tmp) - num1; ans += fig * num2; } } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int ans = 0, x, k = 0; string s; while (getline(cin, s)) { if (s[0] == '+') { k++; continue; } if (s[0] == '-') { k--; continue; } ans += (s.size() - s.find(':') - 1) * k; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long data = 0, ans = 0; string t, m; while (getline(cin, t)) { if (t[0] == '+') data++; else if (t[0] == '-') data--; else { m = ""; for (int i = t.find(':') + 1; i < t.size(); i++) { m += t[i]; } ans += m.size() * data; } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; char c; int s = 0, t = 0; int main() { while (scanf("%c", &c) != -1) { if (c == '+') t++; else if (c == '-') t--; else { while (c != ':') scanf("%c", &c); while (c != '\n') { s += t; scanf("%c", &c); } s -= t; } while (c != '\n') scanf("%c", &c); } printf("%d\n", s); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m = 0, k = 0, l; size_t p; string a, b; while (getline(cin, a)) { if (a[0] == '+') m++; else if (a[0] == '-') m--; else { k += m * (a.size() - a.find(':') - 1); } } cout << k << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int uz, in, x, sayac; int main() { string a; while (getline(cin, a)) { if (a[0] == '+') in++; else if (a[0] == '-') in--; else { sayac += (a.size() - a.find(':') - 1) * in; } } printf("%d", sayac); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string a; int b = 0, k = 0; while (getline(cin, a)) { if (a[0] == '+') k++; else if (a[0] == '-') k--; else b += k * (a.size() - a.find(':') - 1); } cout << b; }
#include <bits/stdc++.h> int main() { using namespace std; char cmd[110]; int num = 0; int numOfOnline = 0; while (gets(cmd)) { if (cmd[0] == '+') numOfOnline++; else if (cmd[0] == '-') numOfOnline--; else { int i; for (i = 0; i < 110; i++) { if (cmd[i] == ':') break; } num += (strlen(cmd) - 1 - i) * numOfOnline; } } printf("%d", num); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; set<string> m; int r = 0; while (getline(cin, s)) { if (s[0] == '+') m.insert(s.substr(1, (int)s.size() - 1)); else if (s[0] == '-') m.erase(m.find(s.substr(1, (int)s.size() - 1))); else r += m.size() * ((int)s.size() - s.find(':') - 1); } cout << r << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; int n = 0, l = 0; while (getline(cin, s)) { if (s[0] == '+') n++; else if (s[0] == '-') n--; else l += (s.length() - s.find(':') - 1) * n; } cout << l; }
#include <bits/stdc++.h> using namespace std; int main() { int i, ans, num; string str; ans = 0; num = 0; while (getline(cin, str)) { if (str[0] == '+') num++; else if (str[0] == '-') num--; else { i = 1; while (str[i] != ':') i++; ans += (str.size() - i - 1) * num; } } printf("%d\n", ans); }
#include <bits/stdc++.h> using namespace std; int main() { string str; int res = 0, count = 0; while (getline(cin, str)) { if (str.length() == 0) break; if (str[0] == '+') count++; else if (str[0] == '-') count--; else { int len = str.length(); res += count * (len - str.find(':') - 1); } } cout << res << endl; }
#include <bits/stdc++.h> using namespace std; int main(void) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n = 0, k, l; string s; long long int res = 0; while (getline(cin, s) && s != "") { if (s[0] == '+') n++; else if (s[0] == '-') n = max(0, n - 1); else { for (long long int(i) = (long long int)(0); (i) < (long long int)(s.length()); (i)++) if (s[i] == ':') { k = i + 1; break; } l = (int)s.length() - k; res += l * n; } } cout << res << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long sum = 0; string s; int c = 0; while (getline(cin, s, '\n')) { if (s[0] == '+') c++; else if (s[0] == '-') c--; else { int t = s.find(":", 0); sum = sum + (s.size() - t - 1) * c; } } cout << sum << endl; }
#include <bits/stdc++.h> using namespace std; int main() { set<string> names; string in; int ans = 0; while (getline(cin, in)) { if (in[0] == '+') names.insert(in.substr(1)); else if (in[0] == '-') names.erase(in.substr(1)); else ans += names.size() * (in.size() - in.find(':', 0) - 1); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #pragma GCC optimze(2) using namespace std; int main() { char s1[101 * 101 * 101] = {}; int i = 1, j, k, s = 0, t = 0; while (gets(s1)) { if (s1[0] == '+') { t++; } else if (s1[0] == '-') { t--; } else { j = 0; while (s1[j] - ':') { j++; } s = s + (strlen(s1 + 1) - j) * t; } } cout << s << endl; return 0; }
#include <bits/stdc++.h> int main() { char str[101], c; int x = 0, i, s = 0, l; while (scanf("%c", &c) != EOF) { if (c == '+') x++; else if (c == '-') x--; gets(str); l = strlen(str); for (i = 0; i < l; i++) { if (str[i] == ':') { s += (l - 1 - i) * x; break; } } } printf("%d\n", s); return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { int traffic = 0, people = 0, msg_size; char str[101]; string s; while (!cin.eof()) { memset(str, 0, 101); cin.getline(str, 101); s.assign(str); if (cin.eof()) break; if (s[0] == '+') people++; else if (s[0] == '-') people--; else { msg_size = s.length() - (s.find_first_of(':') + 1); traffic += people * msg_size; } } cout << traffic << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; int n, a = 0, b, ans = 0; while (getline(cin, s)) { if (s[0] == '+') a++; else if (s[0] == '-') a--; else ans += a * (s.size() - s.find(":") - 1); } cout << ans; }
#include <bits/stdc++.h> using namespace std; int msg(string str) { size_t found; string str2; found = str.find(":"); str2 = str.substr(int(found) + 1); return str2.length(); } int main() { int total = 0, people = 0; string str; while (getline(cin, str)) { if (str[0] == '+') { people++; continue; } else if (str[0] == '-') { people--; continue; } else { total += people * msg(str); } } cout << total; return 0; }
#include <bits/stdc++.h> int main() { char str[1000], a[10]; int len, len1, i, n, m, j, b = 0, k, sum = 0; while (gets(str)) { len = strlen(str); if (str[0] == '+') b++; else if (str[0] == '-') b--; else { for (j = 0; j < len; j++) { if (str[j] == ':') break; } j = j + 1; k = len - j; m = k * b; sum = sum + m; } } printf("%d\n", sum); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char string[101]; int count = 0; int sum = 0; while (cin.getline(string, 101)) { if (string[0] == '+') { count++; continue; } if (string[0] == '-') { count--; continue; } int i = 0; for (; string[i] != ':'; i++) { } sum += count * (strlen(string) - 1 - i); } cout << sum; }
#include <bits/stdc++.h> using namespace std; int n; int main() { string s; int ans = 0; int cnt = 0; while (getline(cin, s)) { if (s[0] == '+') cnt++; else if (s[0] == '-') cnt--; else { for (int i = 0; i < s.length(); i++) if (s[i] == ':') ans += cnt * (s.length() - i - 1); } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename S, typename T> ostream& operator<<(ostream& out, const pair<S, T> p) { out << "(" << p.first << "," << p.second << ")"; return out; } template <typename T> ostream& operator<<(ostream& out, const vector<T>& v) { for (auto a : v) out << a << " "; return out; } template <typename T> ostream& operator<<(ostream& out, const set<T>& S) { for (auto a : S) cout << a << " "; return out; } template <typename T> ostream& operator<<(ostream& out, const multiset<T>& S) { for (auto a : S) cout << a << " "; return out; } template <typename S, typename T> ostream& operator<<(ostream& out, const map<S, T>& M) { for (auto m : M) cout << "(" << m.first << "->" << m.second << ") "; return out; } template <typename S, typename T> pair<S, T> operator+(pair<S, T> a, pair<S, T> b) { return make_pair(a.first + b.first, a.second + b.second); } template <typename S, typename T> pair<S, T> operator-(pair<S, T> a, pair<S, T> b) { return make_pair(a.first - b.first, a.second - b.second); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); string str; set<string> S; long long ans = 0; while (getline(cin, str)) { if (str[0] == '+') { S.insert(str.substr(1)); } else if (str[0] == '-') { S.erase(str.substr(1)); } else { int p = str.find(':'); int len = str.size() - (p + 1); long long val = 1ll * len * S.size(); ans += val; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; int ans = 0; int num = 0; while (getline(cin, s)) { if (s[0] == '+') { num++; continue; } if (s[0] == '-') { num--; continue; } ans += num * (s.length() - s.find_first_of(":") - 1); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int i, bytes = 0, members = 0, aux, tam, numb; string code; cin >> code; while (!cin.eof()) { if (code[0] == '+') { members++; } else if (code[0] == '-') { members--; } else { for (i = 0; code[i] != ':'; i++) { } aux = i; numb = code.size() - 1; for (i = aux; i < numb; i++) { bytes += members; } } getline(cin, code); } cout << bytes; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char c, b[1000]; int a = 0, ans = 0; while (cin >> c) { if (c == '+') { a++; } if (c == '-') { a--; } if (c == ':') { gets(b); ans += strlen(b) * a; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; } template <typename T> int SIZE(const T &t) { return t.size(); } string to_string(const string s, int x1 = 0, int x2 = 1e9) { return '"' + ((x1 < (int)s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c) { return string({c}); } template <size_t N> string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) { string t = ""; for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1); __iii__ <= __jjj__; ++__iii__) { t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A(&v), int x1 = 0, int x2 = 1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A(&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if (l_v_l_v_l == 0) res += '\n'; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2 - x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if (e != l) { if (rnk > 1) { res += '\n'; t_a_b_s = l_v_l_v_l; }; } else { t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if (l_v_l_v_l == 0) res += '\n'; return res; } void dbgm() { ; } template <typename Heads, typename... Tails> void dbgm(Heads H, Tails... T) { cerr << to_string(H) << " | "; dbgm(T...); } double eps = 1e-9; double PI = acos(-1); const int MOD = 1000000007; void solve() { string s; int curr = 0; int ans = 0; while (getline(cin, s)) { cerr << "------------" << '\n' << __func__ << ":" << 177 << " ==> " << "[" << "s" << "]: "; cerr << to_string(s) << '\n'; if (*(s.begin()) == '+') { curr++; } else if (*(s.begin()) == '-') { curr--; } else { auto place = s.find(':'); string second = s.substr(place + 1); cerr << "------------" << '\n' << __func__ << ":" << 185 << " ==> " << "[" << "ss" << "]: "; cerr << to_string(second) << '\n'; ans = ans + curr * second.size(); } } cout << ans << '\n'; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(20); int cases; cases = 1; for (int i = 1; i <= cases; i++) { solve(); } }
#include <bits/stdc++.h> using namespace std; char s[110]; int main() { int len, i, sum, ans; sum = 0; ans = 0; while (gets(s)) { len = strlen(s); if (s[0] == '+') { sum++; } else if (s[0] == '-') { sum--; } else { for (i = 0; i < len; i++) { if (s[i] == ':') { break; } } ans += (len - 1 - i) * sum; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char a[100]; char b; int count = 0; int cc = 0; while (gets(a)) { if (a[0] == '+') count++; else if (a[0] == '-') count--; else { int ccc = 0; for (int i = strlen(a) - 1; i >= 0; i--) { if (a[i] == ':') break; ccc++; } cc = cc + ccc * count; } } cout << cc << endl; return 0; }
#include <bits/stdc++.h> using namespace std; string to(long long a) { if (a == 0) return "0"; bool b = false; if (a < 0) { b = true; a *= -1; } string s = ""; while (a != 0) { s += char(a % 10 + '0'); a /= 10; } string t = ""; for (int i = s.size() - 1; i >= 0; i--) { t += s[i]; } if (b) { string m = ""; m += "-"; m += t; return m; } return t; } void read(vector<int> &a, int n) { a.resize(n); for (int i = 0; i < a.size(); i++) { scanf("%d", &a[i]); } } int main() { set<string> se; int res = 0; string s; while (getline(cin, s)) { if (s[0] == '+') { se.insert(s.substr(1, s.size() - 1)); } else if (s[0] == '-') { se.erase(se.find(s.substr(1, s.size() - 1))); } else { for (int i = 0; i < s.size(); i++) { if (s[i] == ':') { res += (s.size() - 1 - i) * se.size(); break; } } } } cout << res; }
#include <bits/stdc++.h> using namespace std; int a, b; string s; int main() { string s; int cnt = 0, i, j = 0, g; while (getline(cin, s)) { if (s[0] == '-') j--; else if (s[0] == '+') j++; else { i = 0; while (s[i] != ':') i++; cnt += (s.size() - i - 1) * j; } } cout << cnt; return 0; }