제출 #1348460

#제출 시각아이디문제언어결과실행 시간메모리
1348460vjudge1Palembang Bridges (APIO15_bridge)C++20
100 / 100
77 ms4020 KiB
#include <bits/stdc++.h>
#define NAME "code"
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
using namespace std;

const int N= 1e5;
int k, n;

int m;
pii cross[N + 5];
ll prf[N + 5], suf[N + 5];

void build(ll sum[]) {
    priority_queue<int> L;
    priority_queue<int, vector<int>, greater<int>> R;
    ll sL= 0, sR= 0;
    for(int i= 1; i <= m; ++i) {
        auto [u, v]= cross[i];
        sL += u + v;
        L.push(u), L.push(v);
        for(int j= 0; j < min(2, i - 1); ++j) {
            sL += R.top(), L.push(R.top());
            sR -= R.top(), R.pop();
        }
        for(int j= 0; j < min(2, i - 1) + 1; ++j) {
            sR += L.top(), R.push(L.top());
            sL -= L.top(), L.pop();
        }
        sum[i]= sR - sL;
    }
}

void solve() {
    ll ans= 0;
    cin >> k >> n;
    for(int i= 1; i <= n; ++i) {
        int s, t;
        char p, q;
        cin >> p >> s >> q >> t;
        if(s > t) swap(s, t);
        if(p == q) ans += t - s;
        else ++ans, cross[++m]= {s, t};
    }

    sort(cross + 1, cross + 1 + m, [](pii c1, pii c2) -> bool {
        auto [x1, y1]= c1;
        auto [x2, y2]= c2;
        return x1 + y1 < x2 + y2;
    });

    build(prf);
    if(k == 1) ans += prf[m];
    else {
        reverse(cross + 1, cross + 1 + m);
        build(suf);
        ll res= prf[1] + suf[m - 1];
        for(int i= 2; i < m; ++i)
            res= min(res, prf[i] + suf[m - i]);
        ans += res;
    }

    cout << ans << '\n';
}

signed main() {
    if(fopen(NAME".inp", "r")) {
        freopen(NAME".inp", "r", stdin);
        freopen(NAME".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int tests= 1;
    for(int i= 0; i < tests; ++i) solve();

    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

bridge.cpp: In function 'int main()':
bridge.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |         freopen(NAME".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         freopen(NAME".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...