답안 #932478

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
932478 2024-02-23T13:30:57 Z VMaksimoski008 Palembang Bridges (APIO15_bridge) C++14
100 / 100
118 ms 5572 KB
#include <bits/stdc++.h>

#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
//#define int long long

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;

priority_queue<int> L;
priority_queue<int, vector<int>, greater<int> > R;
ll sumL = 0, sumR = 0;

void insert(int x) {
    int med = (L.empty() ? 1e9 + 1 : L.top());
    if(x <= med) L.push(x), sumL += x;
    else R.push(x), sumR += x;

    if(L.size() > R.size() + 1) {
        R.push(L.top());
        sumL -= L.top();
        sumR += L.top();
        L.pop();
    } else if(R.size() > L.size()) {
        L.push(R.top());
        sumL += R.top();
        sumR -= R.top();
        R.pop();
    }
}

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

    ll ans = 0, ans2 = 0;
    vector<pii> v;
    v.push_back({ 0, 0 });

    for(int i=0; i<n; i++) {
        char a, b;
        int c, d;
        cin >> a >> c >> b >> d;

        if(a == b) {
            ans2 += abs(c - d);
        } else {
            v.push_back({ c, d });
            ans2++;
        }
    }

    if(v.size() == 1) {
        cout << ans2 << '\n';
        return 0;
    }

    if(k == 1) {
        for(int i=1; i<v.size(); i++) {
            insert(v[i].first);
            insert(v[i].second);
        }

        ans = sumR - sumL;
    } else {
        ans = 1e18;

        sort(1+all(v), [&](pii &a, pii &b) { return (a.first + a.second) < (b.first + b.second); });

        vector<ll> pref(v.size());
        for(int i=1; i<v.size(); i++) {
            insert(v[i].first);
            insert(v[i].second);
            pref[i] = sumR - sumL;
        }

        sumL = sumR = 0;
        while(!L.empty()) L.pop();
        while(!R.empty()) R.pop();

        for(int i=v.size()-1; i>0; i--) {
            insert(v[i].first);
            insert(v[i].second);
            ans = min(ans, pref[i-1] + sumR - sumL);
        }
    }

    cout << ans + ans2 << '\n';
    return 0;
}

Compilation message

bridge.cpp: In function 'int32_t main()':
bridge.cpp:70:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |         for(int i=1; i<v.size(); i++) {
      |                      ~^~~~~~~~~
bridge.cpp:82:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |         for(int i=1; i<v.size(); i++) {
      |                      ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 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 344 KB Output is correct
8 Correct 1 ms 600 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
# 결과 실행 시간 메모리 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 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 2 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 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 43 ms 2500 KB Output is correct
13 Correct 76 ms 2760 KB Output is correct
14 Correct 51 ms 2324 KB Output is correct
15 Correct 44 ms 1484 KB Output is correct
16 Correct 56 ms 2960 KB Output is correct
17 Correct 74 ms 2244 KB Output is correct
18 Correct 75 ms 2504 KB Output is correct
19 Correct 74 ms 2848 KB Output is correct
20 Correct 63 ms 2244 KB Output is correct
21 Correct 69 ms 2528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 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 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 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 344 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 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 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 344 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 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 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 344 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 1 ms 352 KB Output is correct
9 Correct 1 ms 352 KB Output is correct
10 Correct 0 ms 360 KB Output is correct
11 Correct 1 ms 360 KB Output is correct
12 Correct 0 ms 360 KB Output is correct
13 Correct 1 ms 360 KB Output is correct
14 Correct 1 ms 360 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 344 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 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 48 ms 3188 KB Output is correct
26 Correct 75 ms 4276 KB Output is correct
27 Correct 105 ms 5012 KB Output is correct
28 Correct 114 ms 5572 KB Output is correct
29 Correct 118 ms 5440 KB Output is correct
30 Correct 58 ms 2756 KB Output is correct
31 Correct 70 ms 4564 KB Output is correct
32 Correct 95 ms 4956 KB Output is correct
33 Correct 93 ms 5312 KB Output is correct
34 Correct 101 ms 5060 KB Output is correct
35 Correct 79 ms 4872 KB Output is correct
36 Correct 106 ms 4852 KB Output is correct
37 Correct 44 ms 3724 KB Output is correct