Submission #884502

# Submission time Handle Problem Language Result Execution time Memory
884502 2023-12-07T14:10:34 Z AnhPham Palembang Bridges (APIO15_bridge) C++17
100 / 100
48 ms 8652 KB
#include <bits/stdc++.h>

#ifdef OP_DEBUG
    #include <algo/debug.h>
#else
    #define debug(...) 26
#endif

using namespace std;

#define int 	long long // maybe tle?
#define sz(v)   (int)(v).size()
#define all(v)  (v).begin(), (v).end()
#define TcT     template <class T

const   int     MOD = (int)1e9 + 7, INF = (int)2e9 + 9, INF64 = (int)4e18 + 18;

TcT>            bool minimize(T &val, const T &upd) { return upd < val ? val = upd, 1 : 0; }
TcT>            bool maximize(T &val, const T &upd) { return upd > val ? val = upd, 1 : 0; }

TcT, class S>   istream &operator >> (istream &scanf, pair <T, S> &u) { return scanf >> u.first >> u.second;        }
TcT, class S>   ostream &operator << (ostream &print, pair <T, S> &u) { return print << u.first << ' ' << u.second; }

void solve();

int32_t main() {
    cin.tie(nullptr), cout.tie(nullptr) -> sync_with_stdio(0);
    int testcases = 1;

#define TC 0
    if (TC) { cin >> testcases; } for (; testcases--;) { solve(); }
}

/* [Pham Hung Anh - 11I - Tran Hung Dao High School for Gifted Student] */

int k, n;
int same_side;
vector <pair <int, int>> opposite;

struct Median {
    int sum = 0;
    priority_queue <int> left;
    priority_queue <int, vector <int>, greater <int>> right;

    void insert(int x) {
        if (left.empty() || x <= left.top()) {
            sum -= x;
            left.push(x);
            if (sz(left) > sz(right) + 1) {
                x = left.top(); left.pop();

                right.push(x);
                sum += 2 * x;
            }
        } else {
            sum += x;
            right.push(x);
            if (sz(right) > sz(left)) {
                x = right.top(); right.pop();

                left.push(x);
                sum -= 2 * x;
            }
        }
    }
};

void solve() {
    cin >> k >> n;

    for (int i = 1; i <= n; ++i) {
        char p, q; int s, t; cin >> p >> s >> q >> t;

        if (s > t)
            swap(s, t);

        if (p == q)
            same_side += t - s;
        else
            opposite.emplace_back(s, t), ++same_side;
    }

    if (opposite.empty())
        return void(cout << same_side << '\n');

    sort(all(opposite), [&](pair <int, int> a, pair <int, int> b) -> bool {
        return a.first + a.second < b.first + b.second;
    });

    n = sz(opposite);

    vector <int> pref(n + 1);
    Median Med;
    for (int i = 0; i < n; ++i) {
        Med.insert(opposite[i].first);
        Med.insert(opposite[i].second);

        pref[i + 1] = Med.sum;
    }

    int ans = pref[n];

    if (k == 2) {
        Med = Median();
        for (int i = n - 1; i >= 0; --i) {
            Med.insert(opposite[i].first);
            Med.insert(opposite[i].second);

            minimize(ans, pref[i] + Med.sum);
        }
    }

    cout << ans + same_side << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 484 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 17 ms 5760 KB Output is correct
13 Correct 36 ms 5316 KB Output is correct
14 Correct 28 ms 5068 KB Output is correct
15 Correct 21 ms 3544 KB Output is correct
16 Correct 19 ms 5588 KB Output is correct
17 Correct 30 ms 5616 KB Output is correct
18 Correct 20 ms 5836 KB Output is correct
19 Correct 35 ms 5624 KB Output is correct
20 Correct 25 ms 5576 KB Output is correct
21 Correct 33 ms 5744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 544 KB Output is correct
6 Correct 0 ms 452 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 464 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 456 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 356 KB Output is correct
13 Correct 1 ms 352 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 608 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 428 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 24 ms 6352 KB Output is correct
26 Correct 33 ms 6868 KB Output is correct
27 Correct 45 ms 7980 KB Output is correct
28 Correct 47 ms 8232 KB Output is correct
29 Correct 48 ms 8652 KB Output is correct
30 Correct 24 ms 4560 KB Output is correct
31 Correct 21 ms 7372 KB Output is correct
32 Correct 40 ms 8136 KB Output is correct
33 Correct 26 ms 7632 KB Output is correct
34 Correct 46 ms 7844 KB Output is correct
35 Correct 31 ms 7836 KB Output is correct
36 Correct 43 ms 7624 KB Output is correct
37 Correct 20 ms 6360 KB Output is correct