답안 #1075523

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1075523 2024-08-26T07:20:59 Z stdfloat Palembang Bridges (APIO15_bridge) C++17
22 / 100
47 ms 9996 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() - 1; 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()) t1.erase(t1.find(k));
            else t2.erase(t2.find(k));
            g();
        }
    }

    cout << sm + mn;
}
# 결과 실행 시간 메모리 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 600 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 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
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 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 348 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 1 ms 348 KB Output is correct
10 Correct 1 ms 528 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 37 ms 8460 KB Output is correct
13 Correct 35 ms 9936 KB Output is correct
14 Correct 47 ms 8160 KB Output is correct
15 Correct 21 ms 5904 KB Output is correct
16 Correct 30 ms 9224 KB Output is correct
17 Correct 33 ms 9996 KB Output is correct
18 Correct 34 ms 9460 KB Output is correct
19 Correct 38 ms 9828 KB Output is correct
20 Correct 31 ms 9500 KB Output is correct
21 Correct 33 ms 9800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 456 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 388 KB Output isn't correct
4 Halted 0 ms 0 KB -