Submission #549749

# Submission time Handle Problem Language Result Execution time Memory
549749 2022-04-16T12:48:52 Z Jomnoi Palembang Bridges (APIO15_bridge) C++17
100 / 100
92 ms 5744 KB
#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;

const int MAX_N = 1e5 + 10;
const int INF = 1e9 + 7;

long long pre[MAX_N];
long long suml, sumr;
priority_queue <int> pql;
priority_queue <int, vector <int>, greater <int>> pqr;

void add(const int &v) {
    int median = INF;
    if(!pql.empty()) {
        median = pql.top();
    }

    if(v <= median) {
        suml += v;
        pql.push(v);
    }
    else {
        sumr += v;
        pqr.push(v);
    }

    if(pql.size() > pqr.size() + 1) {
        pqr.push(pql.top());
        sumr += pql.top();
        suml -= pql.top();
        pql.pop();
    }
    else if(pql.size() < pqr.size()) {
        pql.push(pqr.top());
        sumr -= pqr.top();
        suml += pqr.top();
        pqr.pop();
    }
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int K, N;
    cin >> K >> N;
    vector <pair <int, int>> vec({make_pair(-INF, -INF)});
    long long same_side = 0;
    for(int i = 1; i <= N; i++) {
        char p, q;
        int s, t;
        cin >> p >> s >> q >> t;
        if(p == q) {
            same_side += abs(s - t);
        }
        else {
            vec.emplace_back(s, t);
        }
    }

    sort(vec.begin(), vec.end(), [&](const pair <int, int> &a, const pair <int, int> &b) {
        return a.first + a.second < b.first + b.second;
    });

    N = vec.size() - 1;
    same_side += N;
    for(int i = 1; i <= N; i++) {
        add(vec[i].first), add(vec[i].second);
        pre[i] = sumr - suml;
    }

    long long ans = pre[N];

    if(K == 2) {
        while(!pql.empty()) {
            pql.pop();
        }
        while(!pqr.empty()) {
            pqr.pop();
        }
        suml = sumr = 0;

        for(int i = N; i >= 1; i--) {
            add(vec[i].first), add(vec[i].second);
            ans = min(ans, pre[i - 1] + sumr - suml);
        }
    }

    cout << ans + same_side;
    return 0;
}
# 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 1 ms 340 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 340 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 340 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 340 KB Output is correct
4 Correct 1 ms 340 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 340 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 340 KB Output is correct
12 Correct 25 ms 3152 KB Output is correct
13 Correct 49 ms 3164 KB Output is correct
14 Correct 38 ms 3288 KB Output is correct
15 Correct 33 ms 2232 KB Output is correct
16 Correct 33 ms 3216 KB Output is correct
17 Correct 36 ms 3016 KB Output is correct
18 Correct 37 ms 3516 KB Output is correct
19 Correct 41 ms 3056 KB Output is correct
20 Correct 36 ms 3524 KB Output is correct
21 Correct 41 ms 3084 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 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 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 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 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 340 KB Output is correct
20 Correct 1 ms 372 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 364 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 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 1 ms 336 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 212 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 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 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 364 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 368 KB Output is correct
22 Correct 1 ms 356 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 33 ms 3900 KB Output is correct
26 Correct 62 ms 4544 KB Output is correct
27 Correct 87 ms 5060 KB Output is correct
28 Correct 92 ms 5744 KB Output is correct
29 Correct 88 ms 5460 KB Output is correct
30 Correct 44 ms 3020 KB Output is correct
31 Correct 40 ms 4752 KB Output is correct
32 Correct 65 ms 5396 KB Output is correct
33 Correct 68 ms 5000 KB Output is correct
34 Correct 63 ms 5352 KB Output is correct
35 Correct 46 ms 5320 KB Output is correct
36 Correct 62 ms 5104 KB Output is correct
37 Correct 28 ms 3844 KB Output is correct