Submission #114141

# Submission time Handle Problem Language Result Execution time Memory
114141 2019-05-30T14:27:19 Z fedoseevtimofey Palembang Bridges (APIO15_bridge) C++14
100 / 100
348 ms 25572 KB
#include <bits/stdc++.h>

#define int long long

using namespace std;

typedef long long ll;
typedef long double ld;

struct triple {
    int a, b, c;
    bool operator <(const triple &other) const {
        return a < other.a;
    }
};

struct median {
    multiset <int> a, b;
    ll sa, sb;
    void push(int x) {
        if (b.empty() || x >= *b.begin()) {
            b.insert(x);
            sb += x;
        }
        else {
            a.insert(x);
            sa += x;
        }
        while (a.size() > b.size()) {
            int c = *a.rbegin();
            sa -= c;
            sb += c;
            a.erase(--a.end());
            b.insert(c);
        }
        while (b.size() > a.size() + 1) {
            int c = *b.begin();
            sb -= c;
            sa += c;
            b.erase(b.begin());
            a.insert(c);
        }
    }
    ll get() {
        int m = *b.begin();
        return (ll)m * a.size() - sa + sb - (ll)m * b.size();
    }

    median() {
        sa = sb = 0;
    }

};

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20);
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int k, n;
    cin >> k >> n;
    if (k == 1) {
        ll res = 0;
        vector <int> a;
        for (int i = 0; i < n; ++i) {
            char p, q;
            int x, y;
            cin >> p >> x >> q >> y;
            if (p != q) {
                ++res;
                a.push_back(x);
                a.push_back(y);
            }
            else res += abs(x - y);
        }
        sort(a.begin(), a.end());
        int x = a[(int)a.size() / 2];
        for (auto y : a) res += abs(x - y);
        cout << res << '\n';
    }
    else {
        ll res = 0;
        vector <triple> a;
        for (int i = 0; i < n; ++i) {
            char p, q;
            int x, y;
            cin >> p >> x >> q >> y;
            if (p != q) {
                ++res;
                a.push_back({x + y, x, y});
            }
            else res += abs(x - y);
        }
        sort(a.begin(), a.end());
        int m = a.size();
        vector <ll> f(m + 1), s(m + 1);
        median P;
        for (int i = 0; i < m; ++i) {
            P.push(a[i].b);
            P.push(a[i].c);
            f[i + 1] = P.get();
        }
        median S;
        for (int i = m - 1; i >= 0; --i) {
            S.push(a[i].b);
            S.push(a[i].c);
            s[i] = S.get();
        }
        ll ans = 1e18;
        for (int i = 0; i < m + 1; ++i) ans = min(ans, res + f[i] + s[i]);
        cout << ans << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 22 ms 2552 KB Output is correct
13 Correct 46 ms 2544 KB Output is correct
14 Correct 32 ms 2544 KB Output is correct
15 Correct 26 ms 1532 KB Output is correct
16 Correct 27 ms 2580 KB Output is correct
17 Correct 33 ms 2552 KB Output is correct
18 Correct 37 ms 2552 KB Output is correct
19 Correct 42 ms 2552 KB Output is correct
20 Correct 31 ms 2552 KB Output is correct
21 Correct 38 ms 2552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 3 ms 640 KB Output is correct
15 Correct 3 ms 640 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 3 ms 512 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 3 ms 640 KB Output is correct
20 Correct 3 ms 640 KB Output is correct
21 Correct 4 ms 640 KB Output is correct
22 Correct 3 ms 640 KB Output is correct
23 Correct 3 ms 640 KB Output is correct
24 Correct 3 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 3 ms 640 KB Output is correct
14 Correct 3 ms 640 KB Output is correct
15 Correct 3 ms 640 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 3 ms 512 KB Output is correct
18 Correct 2 ms 512 KB Output is correct
19 Correct 3 ms 512 KB Output is correct
20 Correct 3 ms 640 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 3 ms 624 KB Output is correct
23 Correct 3 ms 640 KB Output is correct
24 Correct 4 ms 640 KB Output is correct
25 Correct 163 ms 23904 KB Output is correct
26 Correct 267 ms 24124 KB Output is correct
27 Correct 334 ms 24928 KB Output is correct
28 Correct 348 ms 25568 KB Output is correct
29 Correct 347 ms 25448 KB Output is correct
30 Correct 148 ms 13796 KB Output is correct
31 Correct 174 ms 24752 KB Output is correct
32 Correct 157 ms 25572 KB Output is correct
33 Correct 144 ms 25032 KB Output is correct
34 Correct 161 ms 25492 KB Output is correct
35 Correct 170 ms 25052 KB Output is correct
36 Correct 157 ms 25056 KB Output is correct
37 Correct 175 ms 23832 KB Output is correct