Submission #552264

# Submission time Handle Problem Language Result Execution time Memory
552264 2022-04-23T00:33:38 Z Blossomstream Palembang Bridges (APIO15_bridge) C++14
100 / 100
391 ms 14396 KB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int n, k;
vector<pair<int, int> > v;
int a[100000];
int b[100000];
int main() {
    cin >> k >> n;
    int c = 0;
    ll s = 0;
    for(int i = 0; i < n; i++) {
        int h, t;
        char p, q;
        cin >> p >> h >> q >> t;
        if(p != q) {
            v.push_back(make_pair(h + t, c));
            a[c] = min(h, t);
            b[c] = max(h, t);
            c++;
        } else {
            s += (ll) abs(h - t);
        }
    }
    if(c == 0) {
        cout << s << endl;
        return 0;
    }
    s += (ll) v.size();
    sort(v.begin(), v.end());
    vector<int> w;
    multiset<int> m1;
    multiset<int> m2;
    for(int i = 0; i < c; i++) {
        w.push_back(a[i]);
        w.push_back(b[i]);
        m2.insert(a[i]);
        m2.insert(b[i]);
    }
    sort(w.begin(), w.end());
    int y = w[c - 1];
    int x = -1;
    int r1 = 0, r = 0, l = 0;
    for(int i = 0; i < 2*c; i++) {
        s += (ll) abs(w[i] - y);
        if(w[i] == y) {
            if(i > c - 1) r1++;
            else if(i < c - 1) l++;
        }
    }
    if(k == 1) {
        cout << s << endl;
        return 0;
    }
    ll ans = s;
    for(int j = 0; j < c - 1; j++) {
        int i = v[j].second;
        m1.insert(a[i]);
        m1.insert(b[i]);
        m2.erase(m2.find(a[i]));
        m2.erase(m2.find(b[i]));
        if(a[i] > b[i]) swap(a[i], b[i]);
        if(a[i] > x ) {
            if(r > 0) {
                r--;
            } else {
                x = *m1.upper_bound(x);
                r = m1.count(x) - 1;
            }
            s += ((ll) a[i] + b[i] - 2*x);
        } else if(b[i] >= x && a[i] <= x) {
            if(b[i] == x) r++;
            s += ((ll) b[i] - a[i]);
        } else if(a[i] == x) {
            s += ((ll) b[i] - x);
        }
        if(b[i] < y) {
            if(r1 > 0) {
                r1--;
                l++;
            } else {
                y = *m2.upper_bound(y);
                r1 = m2.count(y) - 1;
                l = 0;
            }
            s -= ((ll) 2*y - a[i] - b[i]);
        } else if(b[i] > y && a[i] <= y) {
            s -= ((ll) b[i] - a[i]);
            if(a[i] == y) {
                if(l > 0) l--;
                else {
                    y = *prev(m2.lower_bound(y));
                    r1 = 0;
                    l = m2.count(y) - 1;
                }
            }
        } else if(b[i] == y) {
            if(r1 > 0) {
                r1--;
                if(a[i] == y) {
                    if(l > 0) l--;
                    else {
                        y = *prev(m2.lower_bound(y));
                        r1 = 0;
                        l = m2.count(y) - 1;
                    }
                }
            } else {
                y = *m2.upper_bound(y);
                r1 = m2.count(y) - 1;
                l = 0;
            }
            s -= ((ll) 2*y - a[i] - b[i]);
        }
        ans = min(ans, s);
    }
    cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 0 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 2 ms 340 KB Output is correct
5 Correct 2 ms 344 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 444 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 2 ms 340 KB Output is correct
5 Correct 2 ms 452 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 ms 352 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 2 ms 456 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 87 ms 12888 KB Output is correct
13 Correct 186 ms 14396 KB Output is correct
14 Correct 167 ms 12208 KB Output is correct
15 Correct 105 ms 8596 KB Output is correct
16 Correct 118 ms 13648 KB Output is correct
17 Correct 134 ms 14364 KB Output is correct
18 Correct 125 ms 13980 KB Output is correct
19 Correct 159 ms 14360 KB Output is correct
20 Correct 123 ms 13864 KB Output is correct
21 Correct 142 ms 14020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 304 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 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 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 312 KB Output is correct
12 Correct 1 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 0 ms 312 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 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 2 ms 444 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 3 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 340 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 3 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 308 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 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 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 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 352 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 448 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 448 KB Output is correct
25 Correct 131 ms 12824 KB Output is correct
26 Correct 249 ms 13020 KB Output is correct
27 Correct 365 ms 13736 KB Output is correct
28 Correct 391 ms 14252 KB Output is correct
29 Correct 379 ms 14340 KB Output is correct
30 Correct 148 ms 7720 KB Output is correct
31 Correct 179 ms 13720 KB Output is correct
32 Correct 216 ms 14308 KB Output is correct
33 Correct 195 ms 13896 KB Output is correct
34 Correct 242 ms 14396 KB Output is correct
35 Correct 171 ms 13924 KB Output is correct
36 Correct 192 ms 14120 KB Output is correct
37 Correct 143 ms 12888 KB Output is correct