Submission #1105170

# Submission time Handle Problem Language Result Execution time Memory
1105170 2024-10-25T15:44:12 Z michified Palembang Bridges (APIO15_bridge) C++17
100 / 100
149 ms 11708 KB
#include <bits/stdc++.h>
#define ll long long
#define db double
#define imx INT_MAX
#define imn INT_MIN
#define lmx LLONG_MAX
#define lmn LLONG_MIN
#define lid id * 2 + 1
#define rid id * 2 + 2
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> 
#define ordered_llset tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> 

const ll mod = 1e9 + 7;

struct pair_t {
    ll l, r;
};

int main() {
    // ifstream cin("fortmoo.in");
    // ofstream cout("fortmoo.out");

    ll k, n, i, s, t;
    cin >> k >> n;
    char p, q;
    ll ans = 0;
    vector<pair_t> pairs;
    for (i = 0; i < n; i++) {
        cin >> p >> s >> q >> t;
        if (s > t) swap(s, t);
        ans += t - s;
        if (p != q) {
            pairs.push_back({s, t});
            ans++;
        }
    }
    sort(pairs.begin(), pairs.end(), [](const pair_t& a, const pair_t& b){return a.l + a.r == b.l + b.r ? a.l < b.l : a.l + a.r < b.l + b.r;});
    n = pairs.size();
    if (n == 0) {
        cout << ans;
        return 0;
    }
    vector<ll> lbest(n), rbest(n);
    priority_queue<ll> lo;
    priority_queue<ll, vector<ll>, greater<ll>> hi;
    auto cmp1 = [](const pair_t& a, const pair_t& b){return a.l > b.l;};
    auto cmp2 = [](const pair_t& a, const pair_t& b){return a.r > b.r;};
    priority_queue<pair_t, vector<pair_t>, decltype(cmp2)> mid(cmp2);
    priority_queue<pair_t, vector<pair_t>, decltype(cmp1)> right(cmp1);
    ll leftcnt = 0, cur = 0, prevmed = 0, median;
    for (i = 0; i < n; i++) {
        hi.push(pairs[i].l);
        hi.push(pairs[i].r);
        while (hi.size() > lo.size()) {
            lo.push(hi.top());
            hi.pop();
        }
        median = lo.top();
        cur += (median - prevmed) * leftcnt * 2;
        cur -= (median - prevmed) * right.size() * 2;
        if (median >= pairs[i].r) {
            leftcnt++;
            cur += abs(median - pairs[i].r) * 2;
        } else if (median < pairs[i].l) {
            right.push(pairs[i]);
            cur += abs(pairs[i].l - median) * 2;
        } else mid.push(pairs[i]);
        while (not right.empty() and median > right.top().l) {
            cur += (median - right.top().l) * 2;
            mid.push(right.top());
            right.pop();
        }
        while (not mid.empty() and median > mid.top().r) {
            cur += (median - mid.top().r) * 2;
            leftcnt++;
            mid.pop();
        }
        lbest[i] = cur;
        prevmed = median;
    }

    reverse(pairs.begin(), pairs.end());
    for (i = 0; i < n; i++) {
        ll tmp = -pairs[i].l;
        pairs[i].l = -pairs[i].r + (ll) 1e9;
        pairs[i].r = tmp + (ll) 1e9;
    }
    while (not lo.empty()) lo.pop();
    while (not hi.empty()) hi.pop();
    while (not mid.empty()) mid.pop();
    while (not right.empty()) right.pop();
    leftcnt = 0;
    cur = 0;
    prevmed = 0;
    for (i = 0; i < n; i++) {
        hi.push(pairs[i].l);
        hi.push(pairs[i].r);
        while (hi.size() > lo.size()) {
            lo.push(hi.top());
            hi.pop();
        }
        median = lo.top();
        cur += (median - prevmed) * leftcnt * 2;
        cur -= (median - prevmed) * (ll) right.size() * 2;
        if (median >= pairs[i].r) {
            leftcnt++;
            cur += abs(median - pairs[i].r) * 2;
        } else if (median < pairs[i].l) {
            right.push(pairs[i]);
            cur += abs(pairs[i].l - median) * 2;
        } else mid.push(pairs[i]);
        while (not right.empty() and median > right.top().l) {
            cur += (median - right.top().l) * 2;
            mid.push(right.top());
            right.pop();
        }
        while (not mid.empty() and median > mid.top().r) {
            cur += (median - mid.top().r) * 2;
            leftcnt++;
            mid.pop();
        }
        rbest[n - i - 1] = cur;
        prevmed = median;
    }
    ll add = rbest[0];
    if (k == 1) {
        cout << ans + add;
        return 0;
    }
    for (i = 0; i < n - 1; i++) add = min(add, lbest[i] + rbest[i + 1]);
    cout << ans + add;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 2 ms 336 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 2 ms 336 KB Output is correct
7 Correct 2 ms 336 KB Output is correct
8 Correct 3 ms 336 KB Output is correct
9 Correct 2 ms 336 KB Output is correct
10 Correct 2 ms 336 KB Output is correct
11 Correct 2 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 504 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 336 KB Output is correct
8 Correct 2 ms 336 KB Output is correct
9 Correct 2 ms 336 KB Output is correct
10 Correct 3 ms 336 KB Output is correct
11 Correct 2 ms 336 KB Output is correct
12 Correct 72 ms 8964 KB Output is correct
13 Correct 149 ms 9900 KB Output is correct
14 Correct 98 ms 8376 KB Output is correct
15 Correct 76 ms 5564 KB Output is correct
16 Correct 74 ms 11532 KB Output is correct
17 Correct 137 ms 9912 KB Output is correct
18 Correct 91 ms 11704 KB Output is correct
19 Correct 112 ms 9912 KB Output is correct
20 Correct 83 ms 11708 KB Output is correct
21 Correct 98 ms 9696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 612 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 592 KB Output is correct
11 Correct 1 ms 404 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 508 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 504 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 508 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 3 ms 336 KB Output is correct
15 Correct 3 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 2 ms 336 KB Output is correct
19 Correct 2 ms 548 KB Output is correct
20 Correct 2 ms 336 KB Output is correct
21 Correct 2 ms 336 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 2 ms 488 KB Output is correct
24 Correct 2 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 504 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 2 ms 336 KB Output is correct
15 Correct 2 ms 336 KB Output is correct
16 Correct 1 ms 508 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 2 ms 508 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 2 ms 336 KB Output is correct
25 Correct 53 ms 8548 KB Output is correct
26 Correct 80 ms 8640 KB Output is correct
27 Correct 113 ms 9400 KB Output is correct
28 Correct 125 ms 9912 KB Output is correct
29 Correct 125 ms 9980 KB Output is correct
30 Correct 66 ms 5052 KB Output is correct
31 Correct 67 ms 11456 KB Output is correct
32 Correct 96 ms 9912 KB Output is correct
33 Correct 85 ms 11684 KB Output is correct
34 Correct 113 ms 9912 KB Output is correct
35 Correct 69 ms 11704 KB Output is correct
36 Correct 95 ms 9600 KB Output is correct
37 Correct 40 ms 7608 KB Output is correct