Submission #545982

# Submission time Handle Problem Language Result Execution time Memory
545982 2022-04-05T19:39:42 Z Ooops_sorry Palembang Bridges (APIO15_bridge) C++14
100 / 100
309 ms 15284 KB
#include<bits/stdc++.h>

using namespace std;

#define int long long
#define pb push_back
#define ld double
#define ll __int128

mt19937 rnd(51);

int suml = 0, sumr = 0, ind = 0;
multiset<int> l, r;

void add(int j) {
    if (j < ind) {
        l.insert(j);
        suml += j;
    } else {
        r.insert(j);
        sumr += j;
    }
    if (l.size() > r.size()) {
        int val = *l.rbegin();
        suml -= val;
        l.erase(l.find(val));
        ind = val;
        r.insert(val);
        sumr += val;
    } else if (l.size() < r.size()) {
        int val = *r.begin();
        sumr -= val;
        r.erase(r.find(val));
        ind = val;
        l.insert(val);
        suml += val;
    }
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif // LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int k, n, ans = 0;
    cin >> k >> n;
    vector<pair<int,int>> u;
    for (int i = 0; i < n; i++) {
        char p, q;
        int s, t;
        cin >> p >> s >> q >> t;
        s--, t--;
        if (p == q) {
            ans += abs(s - t);
        } else {
            u.pb({min(s, t), max(s, t)});
            ans++;
        }
    }
    sort(u.begin(), u.end(), [&](pair<int,int> a, pair<int,int> b){return a.first + a.second < b.first + b.second;});
    int m = u.size();
    vector<int> res(m), ress(m);
    for (int i = 0; i < m; i++) {
        add(u[i].first);
        add(u[i].second);
        res[i] = ind * (int)(l.size()) - suml + sumr - (int)(r.size()) * ind;
    }
    suml = 0, sumr = 0, l.clear(), r.clear();
    for (int i = m - 1; i >= 0; i--) {
        add(u[i].first);
        add(u[i].second);
        ress[i] = ind * (int)(l.size()) - suml + sumr - (int)(r.size()) * ind;
    }
    int mn = (res.size() > 0 ? res.back() : 0);
    if (k == 2) {
        for (int i = 1; i < m; i++) {
            mn = min(mn, res[i - 1] + ress[i]);
        }
    }
    cout << ans + mn << endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 444 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 1 ms 448 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 2 ms 464 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 158 ms 13580 KB Output is correct
13 Correct 309 ms 15168 KB Output is correct
14 Correct 212 ms 12748 KB Output is correct
15 Correct 109 ms 9076 KB Output is correct
16 Correct 140 ms 14512 KB Output is correct
17 Correct 179 ms 15192 KB Output is correct
18 Correct 121 ms 14784 KB Output is correct
19 Correct 160 ms 15208 KB Output is correct
20 Correct 155 ms 14736 KB Output is correct
21 Correct 157 ms 14968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 324 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 456 KB Output is correct
14 Correct 2 ms 460 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 464 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 2 ms 368 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 158 ms 13564 KB Output is correct
26 Correct 222 ms 13764 KB Output is correct
27 Correct 271 ms 14580 KB Output is correct
28 Correct 234 ms 15168 KB Output is correct
29 Correct 242 ms 15284 KB Output is correct
30 Correct 123 ms 8180 KB Output is correct
31 Correct 132 ms 14512 KB Output is correct
32 Correct 141 ms 15164 KB Output is correct
33 Correct 141 ms 14872 KB Output is correct
34 Correct 150 ms 15056 KB Output is correct
35 Correct 145 ms 14728 KB Output is correct
36 Correct 159 ms 14804 KB Output is correct
37 Correct 156 ms 13728 KB Output is correct