답안 #1094790

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1094790 2024-09-30T15:01:16 Z DaciejMaciej Palembang Bridges (APIO15_bridge) C++17
100 / 100
65 ms 5852 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef vector<pair<int, int>> VPII;
typedef vector<long long> VL;

#define PB push_back
#define ALL(x) x.begin(), x.end()
#define SPACE ' '
#define LINE '\n'

priority_queue<int> lq;
priority_queue<int, vector<int>, greater<int>> rq;
ll lsum = 0, rsum = 0;

void insert(int x) {
    if (lq.empty() || x <= lq.top()) {
        lq.push(x);
        lsum += x;
    } else {
        rq.push(x);
        rsum += x;
    }

    if (lq.size() > rq.size() + 1) {
        int tmp = lq.top();
        lq.pop();
        lsum -= tmp;
        rq.push(tmp);
        rsum += tmp;
    } else if (rq.size() > lq.size()) {
        int tmp = rq.top();
        rq.pop();
        rsum -= tmp;
        lq.push(tmp);
        lsum += tmp;
    }
}

void solve() {
    int n, k;
    cin >> k >> n;

    ll same = 0;
    VPII vec = {{0, 0}};  // Start with dummy element

    for (int i = 0; i < n; ++i) {
        char a, b;
        int x, y;
        cin >> a >> x >> b >> y;
        if (a == b) {
            same += abs(x - y);
        } else {
            vec.PB({x, y});
        }
    }

    sort(ALL(vec), [](const PII &a, const PII &b) {
        return a.first + a.second < b.first + b.second;
    });

    n = vec.size() - 1;
    
    VL pref(n + 1, 0);

    for (int i = 1; i <= n; ++i) {
        auto [x, y] = vec[i];
        insert(x);
        insert(y);
        pref[i] = rsum - lsum;
    }

    ll ans = pref[n];

    if (k == 2) {
        
        while (!lq.empty()) lq.pop();
        while (!rq.empty()) rq.pop();
        lsum = rsum = 0;

        for (int i = n; i >= 1; --i) {
            auto [x, y] = vec[i];
            insert(x);
            insert(y);
            ans = min(ans, rsum - lsum + pref[i - 1]);
        }
    }

    cout << same + ans + n << LINE;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    // cin >> t;

    while (t--)
        solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 492 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 452 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 17 ms 4056 KB Output is correct
13 Correct 37 ms 5716 KB Output is correct
14 Correct 30 ms 4548 KB Output is correct
15 Correct 21 ms 3708 KB Output is correct
16 Correct 20 ms 4816 KB Output is correct
17 Correct 29 ms 5584 KB Output is correct
18 Correct 30 ms 5712 KB Output is correct
19 Correct 31 ms 5492 KB Output is correct
20 Correct 23 ms 5512 KB Output is correct
21 Correct 30 ms 5240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 456 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 452 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 388 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 352 KB Output is correct
17 Correct 0 ms 352 KB Output is correct
18 Correct 1 ms 352 KB Output is correct
19 Correct 1 ms 352 KB Output is correct
20 Correct 1 ms 352 KB Output is correct
21 Correct 1 ms 352 KB Output is correct
22 Correct 1 ms 352 KB Output is correct
23 Correct 1 ms 352 KB Output is correct
24 Correct 1 ms 352 KB Output is correct
25 Correct 24 ms 3932 KB Output is correct
26 Correct 50 ms 4580 KB Output is correct
27 Correct 59 ms 5084 KB Output is correct
28 Correct 65 ms 5852 KB Output is correct
29 Correct 64 ms 5588 KB Output is correct
30 Correct 32 ms 3248 KB Output is correct
31 Correct 26 ms 4828 KB Output is correct
32 Correct 48 ms 5580 KB Output is correct
33 Correct 48 ms 5084 KB Output is correct
34 Correct 50 ms 5588 KB Output is correct
35 Correct 31 ms 5336 KB Output is correct
36 Correct 48 ms 5344 KB Output is correct
37 Correct 20 ms 4068 KB Output is correct