답안 #866291

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
866291 2023-10-25T19:08:11 Z Dec0Dedd Palembang Bridges (APIO15_bridge) C++14
100 / 100
130 ms 13508 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pii;

const int N = 1e5+10;
const ll INF = 1e18;

ll a[N], b[N], n, k;

struct Median {
    priority_queue<ll> lw;
    priority_queue<ll, vector<ll>, greater<ll>> up;
    ll slw, sup;

    Median(): slw(0), sup(0) {}

    void add(int x) {
        if (!lw.empty() && lw.top() < x) up.push(x), sup+=x;
        else lw.push(x), slw+=x;

        while (up.size() > lw.size()) {
            sup-=up.top(), slw+=up.top();
            lw.push(up.top()), up.pop();
        }

        while (lw.size() > up.size()+1) {
            slw-=lw.top(), sup+=lw.top();
            up.push(lw.top()), lw.pop();
        }
    }

    ll que() {
        if (lw.empty()) return 0;
        ll vl=lw.top();
        return (vl*lw.size()-slw)+(sup-vl*up.size());
    }
};

int main() {
    cin>>k>>n;

    ll ans=0;
    vector<pii> v;
    for (int i=1; i<=n; ++i) {
        string p, s;
        cin>>p>>a[i]>>s>>b[i];

        if (p == s) {
            ans+=abs(a[i]-b[i]);
            continue;
        } ++ans;
        if (a[i] > b[i]) swap(a[i], b[i]);
        v.push_back({a[i]+b[i], i});
    } sort(v.begin(), v.end());

    if (v.empty()) {
        cout<<ans<<"\n";
        return 0;
    }

    // k = 1
    Median med;
    for (auto u : v)  med.add(a[u.second]), med.add(b[u.second]);
    ll ovl=med.que();

    // k = 2
    if (k == 2) {
        ll p[n+1]={}, suf[n+1]={}, sz=v.size();

        Median lf, rf;
        for (int j=0; j<sz; ++j) {
            lf.add(a[v[j].second]), lf.add(b[v[j].second]);
            p[j]=lf.que();
        }
        
        for (int j=sz-1; j>=0; --j) {
            rf.add(a[v[j].second]), rf.add(b[v[j].second]);
            suf[j]=rf.que();
        }

        ll g=INF;
        for (int j=0; j+1<sz; ++j) g=min(g, p[j]+suf[j+1]);
        ans+=min(ovl, g);
    } else ans+=ovl;
    cout<<ans<<"\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 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 600 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 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 1 ms 348 KB Output is correct
4 Correct 1 ms 456 KB Output is correct
5 Correct 1 ms 500 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 452 KB Output is correct
8 Correct 2 ms 712 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 56 ms 7236 KB Output is correct
13 Correct 110 ms 8540 KB Output is correct
14 Correct 69 ms 6712 KB Output is correct
15 Correct 61 ms 5316 KB Output is correct
16 Correct 71 ms 8100 KB Output is correct
17 Correct 97 ms 8664 KB Output is correct
18 Correct 85 ms 8384 KB Output is correct
19 Correct 94 ms 8636 KB Output is correct
20 Correct 81 ms 8304 KB Output is correct
21 Correct 95 ms 8380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 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 1 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 596 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 440 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 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 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 2 ms 344 KB Output is correct
16 Correct 1 ms 352 KB Output is correct
17 Correct 1 ms 348 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 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 352 KB Output is correct
24 Correct 1 ms 352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 352 KB Output is correct
2 Correct 0 ms 444 KB Output is correct
3 Correct 1 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 1 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 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 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 1 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 456 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 456 KB Output is correct
25 Correct 70 ms 12160 KB Output is correct
26 Correct 85 ms 12184 KB Output is correct
27 Correct 111 ms 12648 KB Output is correct
28 Correct 121 ms 12964 KB Output is correct
29 Correct 130 ms 13340 KB Output is correct
30 Correct 62 ms 7108 KB Output is correct
31 Correct 72 ms 12772 KB Output is correct
32 Correct 108 ms 13500 KB Output is correct
33 Correct 102 ms 13144 KB Output is correct
34 Correct 117 ms 13508 KB Output is correct
35 Correct 95 ms 13060 KB Output is correct
36 Correct 109 ms 13164 KB Output is correct
37 Correct 69 ms 12424 KB Output is correct