Submission #1075643

# Submission time Handle Problem Language Result Execution time Memory
1075643 2024-08-26T08:18:00 Z stdfloat Palembang Bridges (APIO15_bridge) C++17
100 / 100
288 ms 17660 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
 
    int K, n;
    cin >> K >> n;
 
    ll sm = 0;
    vector<vector<int>> v;
    for (int i = 0; i < n; i++) {
        char p, q;
        int s, t;
        cin >> p >> s >> q >> t;
 
        if (p == q) sm += abs(t - s);
        else v.push_back({s + t, s, t});
    }

    if (v.empty()) return cout << sm, 0;
 
    sm += (int)v.size();
 
    if (K == 1) {
        vector<int> u;
        for (auto i : v) u.insert(u.end(), {i[1], i[2]});
        sort(u.begin(), u.end());

        for (int j = 0; j < (int)u.size(); j++) sm += (j < (int)u.size() >> 1 ? -1 : 1) * u[j];

        return cout << sm, 0;
    }

    sort(v.begin(), v.end());
    
    ll ss1 = 0, ss2 = 0, st1 = 0, st2 = 0;
    multiset<int> s1, s2, t1, t2;
    auto g = [&]() {
        if ((int)s1.size() > (int)s2.size() + 1) {
            ss2 += *--s1.end(); ss1 -= *--s1.end();
            s2.insert(*--s1.end()); s1.erase(--s1.end());
        }
        if ((int)s2.size() > (int)s1.size() + 1) {
            ss1 += *s2.begin(); ss2 -= *s2.begin();
            s1.insert(*s2.begin()); s2.erase(s2.begin());
        }

        if ((int)t1.size() > (int)t2.size() + 1) {
            st2 += *--t1.end(); st1 -= *--t1.end();
            t2.insert(*--t1.end()); t1.erase(--t1.end());
        }
        if ((int)t2.size() > (int)t1.size() + 1) {
            st1 += *t2.begin(); st2 -= *t2.begin();
            t1.insert(*t2.begin()); t2.erase(t2.begin());
        }
    };

    ss1 = v[0][1] + v[0][2];
    s1.insert({v[0][1], v[0][2]});
    for (int i = 1; i < (int)v.size(); i++) {
        for (auto k : {v[i][1], v[i][2]}) {
            if (t2.empty() || k < *t2.begin()) {
                st1 += k; t1.insert(k);
            }
            else {
                st2 += k; t2.insert(k);
            }

            g();
        }
    }

    ll mn = LLONG_MAX;
    for (int i = 1; i < (int)v.size(); i++) {
        mn = min(mn, (ll)s1.size() * *--s1.end() - ss1 + ss2 - (ll)s2.size() * *--s1.end() + (ll)t1.size() * *--t1.end() - st1 + st2 - (ll)t2.size() * *--t1.end());
        for (auto k : {v[i][1], v[i][2]}) {
            if (s2.empty() || k < *s2.begin()) {
                ss1 += k; s1.insert(k);
            }
            else {
                ss2 += k; s2.insert(k);
            }
            
            if (t1.find(k) != t1.end()) {
                st1 -= k; t1.erase(t1.find(k));
            }
            else {
                st2 -= k; t2.erase(t2.find(k));
            }
            g();
        }
    }

    cout << sm + mn;
}
# 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 1 ms 348 KB Output is correct
7 Correct 0 ms 532 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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 452 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 532 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 348 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 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 22 ms 8460 KB Output is correct
13 Correct 46 ms 9988 KB Output is correct
14 Correct 27 ms 8200 KB Output is correct
15 Correct 22 ms 5904 KB Output is correct
16 Correct 23 ms 9220 KB Output is correct
17 Correct 32 ms 10032 KB Output is correct
18 Correct 32 ms 9476 KB Output is correct
19 Correct 38 ms 9984 KB Output is correct
20 Correct 26 ms 9472 KB Output is correct
21 Correct 33 ms 9616 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 1 ms 456 KB Output is correct
8 Correct 1 ms 344 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 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 436 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 348 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 2 ms 604 KB Output is correct
15 Correct 2 ms 604 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 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 404 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 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 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 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 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 142 ms 15872 KB Output is correct
26 Correct 177 ms 16248 KB Output is correct
27 Correct 255 ms 16892 KB Output is correct
28 Correct 269 ms 17400 KB Output is correct
29 Correct 288 ms 17484 KB Output is correct
30 Correct 123 ms 9476 KB Output is correct
31 Correct 105 ms 16748 KB Output is correct
32 Correct 129 ms 17400 KB Output is correct
33 Correct 108 ms 17148 KB Output is correct
34 Correct 159 ms 17660 KB Output is correct
35 Correct 147 ms 17076 KB Output is correct
36 Correct 150 ms 17148 KB Output is correct
37 Correct 145 ms 16076 KB Output is correct