Submission #521505

# Submission time Handle Problem Language Result Execution time Memory
521505 2022-02-02T09:32:18 Z Sharky Palembang Bridges (APIO15_bridge) C++17
100 / 100
91 ms 18588 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 100005
#define sz(x) x.size()
#define pq priority_queue
int pref[N], suff[N];
int lps[N], lss[N], rps[N], rss[N], lpcnt[N], lscnt[N], rpcnt[N], rscnt[N];
bool cmp(pair<int, int> x, pair<int, int> y) {
    return x.first + x.second < y.first + y.second;
}
void precompute(vector<pair<int, int> > pv) {
    int nn;
    pq<int> below; pq<int, vector<int>, greater<int> > above;
    int ls = 0, rs = 0;
    for (int i = 1; i < pv.size(); i++) {
        int n = pv[i].first;
        if (below.empty()) {
            below.push(n);
            nn = n;
            ls += n;
        } else if (above.empty()) {
            above.push(n); pref[i] = below.top();
            nn = (above.top() + below.top()) / 2;
            if (above.top() < below.top()) {
                int tmp = below.top();
                int tmp2 = above.top();
                below.pop(); below.push(tmp2);
                above.pop(); above.push(tmp);
                rs += n;
                swap(ls, rs);
            } else {
                rs += n;
            }
        } else {
            if (n <= nn) {
                below.push(n);
                ls += n;
            } else {
                above.push(n);
                rs += n;
            }
            if (sz(below) > sz(above) + 1) {
                int tmp = below.top();
                below.pop();
                above.push(tmp);
                ls -= tmp; rs += tmp;
            } else if (sz(below) < sz(above)) {
                int tmp = above.top();
                above.pop();
                below.push(tmp);
                ls += tmp; rs -= tmp;
            }
            nn = below.top();
        }
        n = pv[i].second;
        if (below.empty()) {
            below.push(n);
            nn = n;
            ls += n;
        } else if (above.empty()) {
            above.push(n); pref[i] = below.top();
            nn = (above.top() + below.top()) / 2;
            if (above.top() < below.top()) {
                int tmp = below.top();
                int tmp2 = above.top();
                below.pop(); below.push(tmp2);
                above.pop(); above.push(tmp);
                rs += n;
                swap(ls, rs);
            } else {
                rs += n;
            }
        } else {
            if (n <= nn) {
                below.push(n);
                ls += n;
            } else {
                above.push(n);
                rs += n;
            }
            if (sz(below) > sz(above) + 1) {
                int tmp = below.top();
                below.pop();
                above.push(tmp);
                ls -= tmp; rs += tmp;
            } else if (sz(below) < sz(above)) {
                int tmp = above.top();
                above.pop();
                below.push(tmp);
                ls += tmp; rs -= tmp;
            }
            nn = below.top();
            pref[i] = nn;
        }
        lps[i] = ls; rps[i] = rs;
        lpcnt[i] = below.size(); rpcnt[i] = above.size();
    }
    ls = 0; rs = 0;
    while (below.size()) below.pop();
    while (above.size()) above.pop();
    for (int i = pv.size() - 1; i >= 1; i--) {
        int n = pv[i].first;
        if (below.empty()) {
            below.push(n);
            nn = n;
            ls += n;
        } else if (above.empty()) {
            above.push(n); suff[i] = below.top();
            nn = (above.top() + below.top()) / 2;
            if (above.top() < below.top()) {
                int tmp = below.top();
                int tmp2 = above.top();
                below.pop(); below.push(tmp2);
                above.pop(); above.push(tmp);
                rs += n;
                swap(ls, rs);
            } else {
                rs += n;
            }
        } else {
            if (n <= nn) {
                below.push(n);
                ls += n;
            } else {
                above.push(n);
                rs += n;
            }
            if (sz(below) > sz(above) + 1) {
                int tmp = below.top();
                below.pop();
                above.push(tmp);
                ls -= tmp; rs += tmp;
            } else if (sz(below) < sz(above)) {
                int tmp = above.top();
                above.pop();
                below.push(tmp);
                ls += tmp; rs -= tmp;
            }
            nn = below.top();
        }
        n = pv[i].second;
        if (below.empty()) {
            below.push(n);
            nn = n;
            ls += n;
        } else if (above.empty()) {
            above.push(n); suff[i] = below.top();
            nn = (above.top() + below.top()) / 2;
            if (above.top() < below.top()) {
                int tmp = below.top();
                int tmp2 = above.top();
                below.pop(); below.push(tmp2);
                above.pop(); above.push(tmp);
                rs += n;
                swap(ls, rs);
            } else {
                rs += n;
            }
        } else {
            if (n <= nn) {
                below.push(n);
                ls += n;
            } else {
                above.push(n);
                rs += n;
            }
            if (sz(below) > sz(above) + 1) {
                int tmp = below.top();
                below.pop();
                above.push(tmp);
                ls -= tmp; rs += tmp;
            } else if (sz(below) < sz(above)) {
                int tmp = above.top();
                above.pop();
                below.push(tmp);
                ls += tmp; rs -= tmp;
            }
            nn = below.top();
            suff[i] = nn;
        }
        lss[i] = ls; rss[i] = rs;
        lscnt[i] = below.size(); rscnt[i] = above.size();
    }
}
signed main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int sameside = 0, k, n, m = 0, ans = 0; cin >> k >> n;
    vector<pair<int, int> > pv; pv.push_back({0, 0});
    vector<int> v(2 * n + 5);
    for (int i = 1; i <= n; i++) {
        char p, q; int s, t; cin >> p >> s >> q >> t;
        if (p == q) sameside += abs(s - t);
        else {
            if (s > t) swap(s, t);
            m++; v[m] = s;
            m++; v[m] = t;
            pv.push_back({s, t});
        }
    }
    sort(pv.begin(), pv.end(), cmp);
    precompute(pv);
    if (k == 1 || m == 2) {
        sort(v.begin() + 1, v.begin() + m + 1);
        int bridge = v[(m + 1) / 2];
        for (int i = 1; i <= m; i += 2) {
            ans += abs(bridge - v[i]) + abs(bridge - v[i + 1]) + 1;
        }
        cout << ans + sameside << "\n";
    } else {
        int mn = 1e18;
        for (int i = 2; i <= (m / 2); i++) {
            ans = (m / 2);
            ans += (lpcnt[i - 1] * pref[i - 1] - lps[i - 1]);
            ans += (rps[i - 1] - pref[i - 1] * rpcnt[i - 1]);
            ans += (lscnt[i] * suff[i] - lss[i]);
            ans += (rss[i] - suff[i] * rscnt[i]);
            mn = min(mn, ans);
        }
        if (m == 0) mn = 0;
        cout << mn + sameside << "\n";
    }
    return 0;
}

/*
2 7
B 1 A 0
A 0 B 0
B 1 B 1
B 1 A 1
B 0 A 1
B 1 A 0
B 0 A 1
*/

Compilation message

bridge.cpp: In function 'void precompute(std::vector<std::pair<long long int, long long int> >)':
bridge.cpp:16:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |     for (int i = 1; i < pv.size(); i++) {
      |                     ~~^~~~~~~~~~~
bridge.cpp:36:13: warning: 'nn' may be used uninitialized in this function [-Wmaybe-uninitialized]
   36 |             if (n <= nn) {
      |             ^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 408 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 38 ms 15760 KB Output is correct
13 Correct 91 ms 15420 KB Output is correct
14 Correct 78 ms 14736 KB Output is correct
15 Correct 53 ms 9600 KB Output is correct
16 Correct 40 ms 15764 KB Output is correct
17 Correct 72 ms 15804 KB Output is correct
18 Correct 56 ms 15684 KB Output is correct
19 Correct 83 ms 15732 KB Output is correct
20 Correct 48 ms 15768 KB Output is correct
21 Correct 79 ms 15740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 36 ms 15744 KB Output is correct
26 Correct 57 ms 16572 KB Output is correct
27 Correct 80 ms 17952 KB Output is correct
28 Correct 81 ms 18516 KB Output is correct
29 Correct 78 ms 18588 KB Output is correct
30 Correct 43 ms 9872 KB Output is correct
31 Correct 37 ms 17404 KB Output is correct
32 Correct 66 ms 18108 KB Output is correct
33 Correct 49 ms 17692 KB Output is correct
34 Correct 74 ms 18072 KB Output is correct
35 Correct 54 ms 17540 KB Output is correct
36 Correct 73 ms 17776 KB Output is correct
37 Correct 35 ms 16520 KB Output is correct