Submission #631441

# Submission time Handle Problem Language Result Execution time Memory
631441 2022-08-18T06:19:17 Z BhavayGoyal Palembang Bridges (APIO15_bridge) C++14
100 / 100
234 ms 15140 KB
#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<class T> using oset = 
            tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"

const int MOD = 1e9+7;
const int inf = 1e18;

const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};


// -------------------------------------------------- Main Code --------------------------------------------------

int a = 0, b = 0;
multiset<int> leftt, rightt;

void add(int x) {
    if (leftt.empty() || x < *leftt.rbegin()) {
        leftt.insert(x);
        a += x;
        if ((leftt.size()+rightt.size())%2 == 0 && leftt.size() > rightt.size()) {
            a -= *leftt.rbegin();
            b += *leftt.rbegin();
            rightt.insert(*leftt.rbegin());
            leftt.erase(leftt.find(*leftt.rbegin()));
        }
    }
    else {
        rightt.insert(x);
        b += x;
        if (rightt.size() > leftt.size()) {
            a += *rightt.begin();
            b -= *rightt.begin();
            leftt.insert(*rightt.begin());
            rightt.erase(rightt.begin());
        }
    }
}

void sol() {
    int k, n; cin >> k >> n;
    vector<array<int, 2>> arr;
    int sameSide = 0;
    for (int i = 0; i < n; i++) {
        char a, b; int x, y;
        cin >> a >> x >> b >> y;
        if (a == b) sameSide += abs(x-y);
        else arr.pb({x, y});
    }
    n = arr.size();
    sameSide += n;
    if (n == 0) { cout << sameSide << endl; return; }
    sort(all(arr), [&](array<int, 2> a, array<int, 2> b) {return a[0]+a[1] < b[0]+b[1];});

    vi pref(n);
    for (int i = 0; i < n; i++) {
        add(arr[i][0]); add(arr[i][1]);
        int median = *leftt.rbegin();
        pref[i] = median*leftt.size() - a + b - median*rightt.size();
    }
    if (k == 1) {cout << pref[n-1]+sameSide << endl;return;}
    leftt.clear(); rightt.clear(); a = 0, b = 0;
    vi suff(n);
    for (int i = n-1; i >= 0; i--) {
        add(arr[i][0]); add(arr[i][1]);
        int median = *leftt.rbegin();
        suff[i] = median*leftt.size() - a + b - median*rightt.size();
    }
    int ans = pref[n-1];
    for (int i = 0; i < n-1; i++) ans = min(ans, pref[i]+suff[i+1]);
    cout << sameSide+ans << endl;
}

signed main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t = 1;
    // cin >> t; 
    while (t--) {
        sol();
    }
    return 0;
}
# Verdict Execution time Memory 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 356 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 2 ms 468 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 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 2 ms 460 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 464 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
12 Correct 79 ms 12712 KB Output is correct
13 Correct 160 ms 14320 KB Output is correct
14 Correct 136 ms 12052 KB Output is correct
15 Correct 60 ms 8548 KB Output is correct
16 Correct 85 ms 13712 KB Output is correct
17 Correct 83 ms 14272 KB Output is correct
18 Correct 67 ms 13940 KB Output is correct
19 Correct 94 ms 14280 KB Output is correct
20 Correct 101 ms 13904 KB Output is correct
21 Correct 81 ms 14072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 320 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 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 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory 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 1 ms 340 KB Output is correct
5 Correct 1 ms 316 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 320 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 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 456 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 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 356 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 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 320 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 240 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 448 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 2 ms 456 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 128 ms 13616 KB Output is correct
26 Correct 185 ms 13760 KB Output is correct
27 Correct 234 ms 14584 KB Output is correct
28 Correct 205 ms 15040 KB Output is correct
29 Correct 232 ms 15140 KB Output is correct
30 Correct 98 ms 8264 KB Output is correct
31 Correct 124 ms 14460 KB Output is correct
32 Correct 131 ms 15100 KB Output is correct
33 Correct 115 ms 14728 KB Output is correct
34 Correct 149 ms 15128 KB Output is correct
35 Correct 134 ms 14656 KB Output is correct
36 Correct 137 ms 14900 KB Output is correct
37 Correct 131 ms 13496 KB Output is correct