Submission #1094817

# Submission time Handle Problem Language Result Execution time Memory
1094817 2024-09-30T15:49:21 Z Octa_pe_info Palembang Bridges (APIO15_bridge) C++14
100 / 100
207 ms 13640 KB
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;

multiset<int> st, dr;
long long ssum = 0, dsum = 0;

bool cmp(pair<int, int> a, pair<int, int> b) {
    return a.first + a.second < b.first + b.second;
}

void adauga(int nr) {
    int mid = st.empty() ? 10000001 : *st.rbegin();

    if (nr <= mid) {
        st.insert(nr);
        ssum += nr;
    } else {
        dr.insert(nr);
        dsum += nr;
    }

    // Balancing the sets
    if (dr.size() + 1 < st.size()) {
        int auxs = *st.rbegin();
        st.erase(st.find(auxs));
        dr.insert(auxs);
        ssum -= auxs;
        dsum += auxs;
    } else if (st.size() < dr.size()) {
        int auxs = *dr.begin();
        dr.erase(dr.find(auxs));
        st.insert(auxs);
        dsum -= auxs;
        ssum += auxs;
    }
}

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

    vector<pair<int, int>> poz;
    long long ans = 0, fel = 0;

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

        if (c == cc)
            fel += abs(a - b);  // If both locations are on the same side
        else
            poz.push_back({a, b});  // If they are on opposite sides, add to the positions
    }

    // If no positions need a bridge, return early
    if (poz.empty()) {
        cout << fel << endl;
        return 0;
    }

    sort(poz.begin(), poz.end(), cmp);
    fel += poz.size();  // Counting the river crossings

    vector<long long> api(poz.size());

    // First pass to compute differences
    for (int i = 0; i < poz.size(); i++) {
        adauga(poz[i].first);
        adauga(poz[i].second);
        api[i] = dsum - ssum;
    }

    ans = api[poz.size() - 1];  // The largest difference at the end

    if (k == 2) {
        // Clear the multisets for the second pass
        st.clear();
        dr.clear();
        ssum = dsum = 0;

        for (int i = poz.size() - 1; i >= 0; i--) {
            adauga(poz[i].first);
            adauga(poz[i].second);
            
            if (i > 0) {  // Avoid accessing api[-1]
                ans = min(ans, dsum - ssum + api[i - 1]);
            }
        }
    }

    cout << ans + fel << endl;

    return 0;
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:71: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]
   71 |     for (int i = 0; i < poz.size(); 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 1 ms 508 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 448 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 2 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 1 ms 604 KB Output is correct
5 Correct 2 ms 352 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 2 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 76 ms 11952 KB Output is correct
13 Correct 140 ms 13640 KB Output is correct
14 Correct 105 ms 11464 KB Output is correct
15 Correct 75 ms 8136 KB Output is correct
16 Correct 86 ms 12996 KB Output is correct
17 Correct 102 ms 13512 KB Output is correct
18 Correct 86 ms 13272 KB Output is correct
19 Correct 110 ms 13540 KB Output is correct
20 Correct 127 ms 13252 KB Output is correct
21 Correct 102 ms 13468 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 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 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 352 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 344 KB Output is correct
9 Correct 1 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 348 KB Output is correct
15 Correct 1 ms 468 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 448 KB Output is correct
20 Correct 2 ms 344 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 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 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 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 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 432 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 436 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 448 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 472 KB Output is correct
23 Correct 1 ms 360 KB Output is correct
24 Correct 1 ms 360 KB Output is correct
25 Correct 120 ms 12124 KB Output is correct
26 Correct 168 ms 12284 KB Output is correct
27 Correct 198 ms 12960 KB Output is correct
28 Correct 206 ms 13628 KB Output is correct
29 Correct 207 ms 13520 KB Output is correct
30 Correct 104 ms 7384 KB Output is correct
31 Correct 119 ms 13032 KB Output is correct
32 Correct 152 ms 13524 KB Output is correct
33 Correct 113 ms 13272 KB Output is correct
34 Correct 139 ms 13508 KB Output is correct
35 Correct 137 ms 13252 KB Output is correct
36 Correct 129 ms 13252 KB Output is correct
37 Correct 112 ms 12172 KB Output is correct