답안 #827043

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
827043 2023-08-16T08:19:00 Z rinho Palembang Bridges (APIO15_bridge) C++14
100 / 100
168 ms 13580 KB
#include <bits/stdc++.h>
using namespace std;

const int Nmax = 3e5;

int n, k, len;
long long ans = 0, suml = 0, sumr = 0, pre[Nmax + 3];
vector<pair<int, int>> val;
multiset<int> up, down;

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

void ins(int x){
    int med;
    if(down.size()) med = *down.rbegin();
    else med = 1e9 + 7;

    if(med < x){
        up.insert(x);
        sumr += 0ll + x;
    } else{
        down.insert(x);
        suml += 0ll + x;
    }

    if(down.size() > up.size() + 1){
        up.insert(*down.rbegin());
        sumr += 0ll + *down.rbegin();
        suml -= 0ll + *down.rbegin();
        down.erase(down.find(*down.rbegin()));
        return;
    }

    if(up.size() > down.size()){
        down.insert(*up.begin());
        suml += 0ll + *up.begin();
        sumr -= 0ll + *up.begin();
        up.erase(up.find(*up.begin()));
    }
}

void in(){
    cin >> k >> n;
    val.push_back({0, 0});
    for(int i = 1; i <= n; i++){
        char p, q;
        int s, t;
        cin >> p >> s >> q >> t;
        if(p == q) ans += 0ll + abs(s - t);
        else val.push_back({s, t});
    }
    sort(val.begin(), val.end(), cmp);
    len = val.size() - 1;
}

void sol(){
    ans += 0ll + len;

    for(int i = 1; i <= len; i++){
        ins(val[i].first);
        ins(val[i].second);
        pre[i] = sumr - suml;
    }

    long long res = pre[len];

    if(k == 2){
        up.clear();
        down.clear();
        sumr = suml = 0;

        for(int i = len; i >= 1; i--){
            ins(val[i].first);
            ins(val[i].second);
            res = min(res, sumr - suml + pre[i - 1]);
        }
    }
    cout << ans + res << '\n';
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    in();
    sol();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 324 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 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 340 KB Output is correct
8 Correct 1 ms 356 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 340 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 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 360 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 61 ms 12036 KB Output is correct
13 Correct 110 ms 13488 KB Output is correct
14 Correct 83 ms 11312 KB Output is correct
15 Correct 50 ms 8036 KB Output is correct
16 Correct 64 ms 12820 KB Output is correct
17 Correct 65 ms 13536 KB Output is correct
18 Correct 72 ms 13140 KB Output is correct
19 Correct 96 ms 13544 KB Output is correct
20 Correct 68 ms 13132 KB Output is correct
21 Correct 75 ms 13268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 0 ms 212 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 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 324 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 328 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 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 1 ms 340 KB Output is correct
20 Correct 1 ms 356 KB Output is correct
21 Correct 2 ms 352 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 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 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 328 KB Output is correct
12 Correct 0 ms 324 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 468 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 468 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 360 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 99 ms 11964 KB Output is correct
26 Correct 158 ms 12228 KB Output is correct
27 Correct 166 ms 12984 KB Output is correct
28 Correct 163 ms 13508 KB Output is correct
29 Correct 168 ms 13544 KB Output is correct
30 Correct 71 ms 7244 KB Output is correct
31 Correct 89 ms 12896 KB Output is correct
32 Correct 113 ms 13580 KB Output is correct
33 Correct 83 ms 13212 KB Output is correct
34 Correct 109 ms 13564 KB Output is correct
35 Correct 107 ms 13128 KB Output is correct
36 Correct 106 ms 13256 KB Output is correct
37 Correct 102 ms 12032 KB Output is correct