답안 #739151

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
739151 2023-05-10T04:06:19 Z Alihan_8 Palembang Bridges (APIO15_bridge) C++17
100 / 100
256 ms 14348 KB
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define pb push_back
#define ln '\n'
#define int long long

template <class _T>
bool chmin(_T &x, const _T &y){
    bool flag = false;
    if ( x > y ){
        x = y; flag |= true;
    }
    return flag;
}

template <class _T>
bool chmax(_T &x, const _T &y){
    bool flag = false;
    if ( x < y ){
        x = y; flag |= true;
    }
    return flag;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int k, n; cin >> k >> n;
    vector <pair<int,int>> p;
    int res = 0;
    for ( int i = 0; i < n; i++ ){
        char a, b;
        int x, y;
        cin >> a >> x >> b >> y;
        if ( x > y ) swap(x, y);
        if ( a == b ){
            res += y - x;
        }
        else{
            p.pb({x, y});
        }
    }
    res += (int)p.size();
    sort(all(p), [&](pair <int,int> &x, pair <int,int> &y){
         return x.first + x.second < y.first + y.second;
    });
    multiset <int> L, R;
    int ls = 0, rs = 0;
    const int inf = 1e16 + 1;
    auto ins = [&](int x){
        int Mx = L.empty() ? inf : *L.rbegin();
        if ( x <= Mx ){
            L.insert(x);
            ls += x;
        }
        else{
            R.insert(x);
            rs += x;
        }
        while ( (int)L.size() > (int)R.size() ){
            auto l = prev(L.end());
            L.erase(l); ls -= *l;
            R.insert(*l); rs += *l;
        }
        while ( (int)R.size() > (int)L.size() ){
            auto r = R.begin();
            R.erase(r); rs -= *r;
            L.insert(*r); ls += *r;
        }
        return rs - ls;
    };
    n = (int)p.size();
    vector <int> pref(n + 1);
    for ( int i = 0; i < n; i++ ){
        auto [l, r] = p[i];
        ins(l);
        pref[i + 1] = ins(r);
    }
    int ans = pref.back();
    if ( k == 2 ){
        L.clear(), R.clear();
        ls = rs = 0;
        for ( int i = n - 1; i >= 0; i-- ){
            auto [l, r] = p[i];
            ins(l);
            chmin(ans, ins(r) + pref[i]);
        }
    }
    cout << ans + res;

    cout << '\n';
}
/*

1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7

2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 1 ms 352 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 356 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 456 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 456 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 92 ms 12764 KB Output is correct
13 Correct 121 ms 12892 KB Output is correct
14 Correct 99 ms 11728 KB Output is correct
15 Correct 64 ms 8036 KB Output is correct
16 Correct 90 ms 12860 KB Output is correct
17 Correct 84 ms 12916 KB Output is correct
18 Correct 70 ms 12948 KB Output is correct
19 Correct 90 ms 12916 KB Output is correct
20 Correct 92 ms 12912 KB Output is correct
21 Correct 87 ms 12956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 324 KB Output is correct
2 Correct 0 ms 316 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 456 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 356 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 156 ms 12916 KB Output is correct
26 Correct 221 ms 13012 KB Output is correct
27 Correct 256 ms 13760 KB Output is correct
28 Correct 238 ms 14320 KB Output is correct
29 Correct 240 ms 14336 KB Output is correct
30 Correct 95 ms 7732 KB Output is correct
31 Correct 160 ms 13668 KB Output is correct
32 Correct 142 ms 14348 KB Output is correct
33 Correct 115 ms 14008 KB Output is correct
34 Correct 147 ms 14320 KB Output is correct
35 Correct 161 ms 13872 KB Output is correct
36 Correct 144 ms 14004 KB Output is correct
37 Correct 166 ms 12784 KB Output is correct