Submission #1054428

# Submission time Handle Problem Language Result Execution time Memory
1054428 2024-08-12T09:47:30 Z unnick Palembang Bridges (APIO15_bridge) C++14
100 / 100
73 ms 4412 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

typedef long long int ll;

int main() {
    int k, n;
    cin >> k >> n;
    if (k != 1) {
        
        vector<pair<int, int> > a;
        ll sum = 0;

        for (int i = 0; i < n; i++) {
            char sa, sb;
            int ia, ib;
            cin >> sa >> ia >> sb >> ib;
            if (sa == sb) {
                sum += abs(ia-ib);
                continue;
            }
            a.push_back(make_pair(ia, ib));
            sum += 1;
        }
        // if (a.size() == 1) {
        //     cout << sum + abs(a[0].first - a[0].second) << "\n";
        // }
        sort(a.begin(), a.end(), [](auto a, auto b){
            return a.first + a.second < b.first + b.second;
        });

        n = a.size();

        vector<ll> lh_s(n+1);
        vector<ll> hh_s(n+1);

        // for (auto b : a) {
        //     cout << b.first << " " << b.second << "\n";
        // }

        for (int q = 0; q < 2; q++) {
            // cout << "== " << q << "\n";
            if (q == 1) {
                for (int i = 0; i < a.size()/2; i++) {
                    auto tmp = a[i];
                    a[i] = a[a.size()-i-1];
                    a[a.size()-i-1] = tmp;
                }
            }
            ll ls = 0, hs = 0;
            priority_queue<int> lh;
            priority_queue<int, vector<int>, greater<int> > hh;

            bool fst = true;
            int ptr = q == 0 ? 0 : a.size();
            for (auto b : a) {
                // cout << "=" << ls << " " << hs << "\n";
                if (q == 0) lh_s[ptr++] = hs - ls; else hh_s[ptr--] = hs - ls;
                if (fst) {
                    lh.push(min(b.first, b.second));
                    hh.push(max(b.first, b.second));
                    ls = min(b.first, b.second);
                    hs = max(b.first, b.second);
                    fst = false;
                    continue;
                }
                if (b.first > lh.top()) {
                    hh.push(b.first);
                    hs += b.first;
                } else {
                    lh.push(b.first);
                    ls += b.first;
                }
                if (b.second > lh.top()) {
                    hh.push(b.second);
                    hs += b.second;
                } else {
                    lh.push(b.second);
                    ls += b.second;
                }
                if (lh.size() > hh.size()) {
                    int tmp = lh.top();
                    ls -= tmp;
                    hs += tmp;
                    lh.pop();
                    hh.push(tmp);
                } else if (lh.size() < hh.size()) {
                    int tmp = hh.top();
                    hs -= tmp;
                    ls += tmp;
                    hh.pop();
                    lh.push(tmp);
                }
                // cout << b.first << " " << b.second << "\n";
            }
            if (q == 0) lh_s[ptr] = hs - ls; else hh_s[ptr] = hs - ls;
        }
        // for (auto v : lh_s) {
        //     cout << "l" << v << "\n";
        // }
        // for (auto v : hh_s) {
        //     cout << "h" << v << "\n";
        // }

        ll bs = 0x7FFFFFFFFFFFFFFFLL;

        for (int i = 0; i < n+1; i++) {
            bs = min(bs, lh_s[i] + hh_s[i]);
        }

        sum += bs;

        cout << sum << "\n";

        return 0;
    }

    vector<int> a;
    ll sum = 0;

    for (int i = 0; i < n; i++) {
        char sa, sb;
        int ia, ib;
        cin >> sa >> ia >> sb >> ib;
        if (sa == sb) {
            sum += abs(ia-ib);
            continue;
        }
        a.push_back(ia);
        a.push_back(ib);
        sum += 1;
    }

    sort(a.begin(), a.end());
    int p = a[a.size() / 2];

    for (auto v : a) {
        sum += abs(v-p);
    }

    cout << sum << "\n";
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:48:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |                 for (int i = 0; i < a.size()/2; i++) {
      |                                 ~~^~~~~~~~~~~~
# Verdict Execution time Memory 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 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 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
# Verdict Execution time Memory 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 1 ms 348 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 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 30 ms 1464 KB Output is correct
13 Correct 57 ms 1492 KB Output is correct
14 Correct 34 ms 1488 KB Output is correct
15 Correct 33 ms 980 KB Output is correct
16 Correct 38 ms 1492 KB Output is correct
17 Correct 49 ms 1500 KB Output is correct
18 Correct 48 ms 1484 KB Output is correct
19 Correct 55 ms 1480 KB Output is correct
20 Correct 42 ms 1480 KB Output is correct
21 Correct 50 ms 1480 KB Output is correct
# Verdict Execution time Memory 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 1 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 1 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory 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 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 344 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 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 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 0 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 448 KB Output is correct
# Verdict Execution time Memory 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 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 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 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 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 392 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 32 ms 4296 KB Output is correct
26 Correct 45 ms 4056 KB Output is correct
27 Correct 58 ms 4036 KB Output is correct
28 Correct 73 ms 4340 KB Output is correct
29 Correct 67 ms 4200 KB Output is correct
30 Correct 35 ms 2252 KB Output is correct
31 Correct 40 ms 4312 KB Output is correct
32 Correct 61 ms 4032 KB Output is correct
33 Correct 45 ms 4412 KB Output is correct
34 Correct 66 ms 3888 KB Output is correct
35 Correct 48 ms 4412 KB Output is correct
36 Correct 61 ms 3944 KB Output is correct
37 Correct 33 ms 4024 KB Output is correct