답안 #319994

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
319994 2020-11-07T05:08:15 Z thecodingwizard Palembang Bridges (APIO15_bridge) C++11
100 / 100
107 ms 7780 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
#define ii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define F0R(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define inf 1000000010

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int k, n; cin >> k >> n;
    ll ans = 0;
    vector<ii> A;
    for (int i = 0; i < n; i++) {
        char a, b; int c, d; cin >> a >> c >> b >> d;
        if (a == b) ans += abs(d-c);
        else A.pb(mp(min(c, d), max(c, d)));
    }

    sort(all(A), [](ii l, ii r) { return (l.f+l.s) < (r.f + r.s); });
    priority_queue<int> r1, r2;
    ll r1s = 0, r2s = 0;
    vector<ll> costs;
    for (auto x : A) {
        r1.push(x.f); r2.push(-x.s);
        int mid = r1.top();
        r1s += x.f; r2s += x.s;
        if (mid > -r2.top()) {
            r2.push(-mid);
            r2s += mid;
            r1.push(-r2.top());
            r1s += -r2.top();
            r2s -= -r2.top();
            r2.pop();
            r1s -= r1.top();
            r1.pop();
        }
        costs.pb(r2s-r1s);
    }
    while (!r1.empty()) r1.pop();
    while (!r2.empty()) r2.pop();
    vector<ll> costs2;
    reverse(all(A));
    r1s = r2s = 0;
    for (auto x : A) {
        r1.push(x.f); r2.push(-x.s);
        int mid = r1.top();
        r1s += x.f; r2s += x.s;
        if (mid > -r2.top()) {
            r2.push(-mid);
            r2s += mid;
            r1.push(-r2.top());
            r1s += -r2.top();
            r2s -= -r2.top();
            r2.pop();
            r1s -= r1.top();
            r1.pop();
        }
        costs2.pb(r2s-r1s);
    }
    if (k == 1) {
        ll output = (ll)ans + (costs.empty() ? 0 : (ll)costs.back()) + (ll)costs.size();
        cout << output << endl;
    } else {
        ll best = (ll)ans + (costs.empty() ? 0 : (ll)costs.back()) + (ll)costs.size();
        for (int i = 0; i < (int)A.size()-1; i++) {
            best = min(best, (ll)ans + (ll)costs.size() + costs[i] + costs2[(ll)A.size()-i-2]);
        }
        cout << best << endl;
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 268 KB Output is correct
2 Correct 1 ms 268 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 2 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 268 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 4 ms 364 KB Output is correct
5 Correct 2 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 42 ms 5112 KB Output is correct
13 Correct 104 ms 4704 KB Output is correct
14 Correct 84 ms 4320 KB Output is correct
15 Correct 56 ms 2788 KB Output is correct
16 Correct 42 ms 5284 KB Output is correct
17 Correct 90 ms 5084 KB Output is correct
18 Correct 54 ms 5216 KB Output is correct
19 Correct 100 ms 5084 KB Output is correct
20 Correct 65 ms 4712 KB Output is correct
21 Correct 107 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 508 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 396 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 512 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 2 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 2 ms 492 KB Output is correct
22 Correct 2 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 2 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 396 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 2 ms 392 KB Output is correct
15 Correct 2 ms 396 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 2 ms 492 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 2 ms 364 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 2 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 2 ms 364 KB Output is correct
25 Correct 41 ms 6260 KB Output is correct
26 Correct 88 ms 5596 KB Output is correct
27 Correct 95 ms 6364 KB Output is correct
28 Correct 100 ms 7008 KB Output is correct
29 Correct 98 ms 7000 KB Output is correct
30 Correct 53 ms 3812 KB Output is correct
31 Correct 42 ms 6880 KB Output is correct
32 Correct 90 ms 7516 KB Output is correct
33 Correct 56 ms 7264 KB Output is correct
34 Correct 102 ms 7780 KB Output is correct
35 Correct 74 ms 6780 KB Output is correct
36 Correct 97 ms 7264 KB Output is correct
37 Correct 35 ms 5984 KB Output is correct