Submission #923092

# Submission time Handle Problem Language Result Execution time Memory
923092 2024-02-06T14:56:07 Z quanlt206 Palembang Bridges (APIO15_bridge) C++17
100 / 100
74 ms 6092 KB
#include<bits/stdc++.h>
#define X first
#define Y second
#define all(x) begin(x), end(x)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define FORD(i, b, a) for(int i = (b); i >= (a); i--)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define mxx max_element
#define mnn min_element
#define SQR(x) (1LL * (x) * (x))
#define MASK(i) (1LL << (i))
#define Point Vector
#define left Left
#define right Right
#define div Div

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<db, db> pdb;
typedef pair<ld, ld> pld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
typedef pair<ll, int> pli;
typedef pair<ll, pii> plii;

template<class A, class B>
    bool maximize(A& x, B y) {
        if (x < y) return x = y, true; else return false;
    }
template<class A, class B>
    bool minimize(A& x, B y) {
        if (x > y) return x = y, true; else return false;
    }
/* END OF TEMPLATE */

const int N = 3e5 + 7;

int n, k;

vector<pii> seg;

ll res = 0;

priority_queue<int, vector<int>> left;
priority_queue<int, vector<int>, greater<int>> right;

ll lsum = 0, rsum = 0;

ll pre[N], suf[N];

void insert_value(int x) {
    int median = (left.empty() ? 1000000007 : left.top());
    if (x <= median) {
        left.push(x);
        lsum+=x;
    }
    else {
        right.push(x);
        rsum+=x;
    }
    if (left.size() == right.size()) return ;
    if ((int)left.size() > (int)right.size() + 1) {
        lsum-=left.top();
        rsum+=left.top();
        right.push(left.top());
        left.pop();
    }
    else
    if ((int)left.size() < (int)right.size()) {
        rsum-=right.top();
        lsum+=right.top();
        left.push(right.top());
        right.pop();
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>k>>n;
    FOR(i, 1, n) {
        char P, Q;
        int A, B;
        cin>>P>>A>>Q>>B;
        if (P == Q) res+=abs(A - B);
        if (P != Q) seg.push_back({min(A, B), max(A, B)}), res++;
    }
    if (seg.empty()) return cout<<res, 0;
    seg.push_back({-1, -1});
    sort(all(seg), [](pii a, pii b) {
        return a.X + a.Y < b.X + b.Y;
    });
    pre[0] = 0;
    n = (int)seg.size() - 1;
    FOR(i, 1, n) {
        insert_value(seg[i].X);
        insert_value(seg[i].Y);
        pre[i] = rsum - lsum;
    }
    if (k == 1) return cout<<pre[n] + res, 0;
    while (!left.empty()) left.pop();
    while (!right.empty()) right.pop();
    lsum = 0;
    rsum = 0;
    ll ans = 1e18;
    FORD(i, n, 1) {
        insert_value(seg[i].X);
        insert_value(seg[i].Y);
        minimize(ans, rsum - lsum + res + pre[i - 1]);
    }
    cout<<ans;
    return 0;
}
# 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 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2648 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 1 ms 2648 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2528 KB Output is correct
8 Correct 1 ms 2548 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 17 ms 4568 KB Output is correct
13 Correct 37 ms 5260 KB Output is correct
14 Correct 28 ms 4976 KB Output is correct
15 Correct 21 ms 4048 KB Output is correct
16 Correct 18 ms 4620 KB Output is correct
17 Correct 36 ms 4556 KB Output is correct
18 Correct 20 ms 4816 KB Output is correct
19 Correct 36 ms 5324 KB Output is correct
20 Correct 31 ms 5072 KB Output is correct
21 Correct 32 ms 4808 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 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2648 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 0 ms 2396 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 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 1 ms 2648 KB Output is correct
14 Correct 1 ms 2536 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 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 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2516 KB Output is correct
17 Correct 1 ms 2392 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 1 ms 2392 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 25 ms 4568 KB Output is correct
26 Correct 45 ms 4696 KB Output is correct
27 Correct 60 ms 5316 KB Output is correct
28 Correct 61 ms 6092 KB Output is correct
29 Correct 74 ms 4884 KB Output is correct
30 Correct 32 ms 3884 KB Output is correct
31 Correct 23 ms 5080 KB Output is correct
32 Correct 57 ms 4560 KB Output is correct
33 Correct 35 ms 4812 KB Output is correct
34 Correct 55 ms 5372 KB Output is correct
35 Correct 35 ms 5096 KB Output is correct
36 Correct 54 ms 4568 KB Output is correct
37 Correct 21 ms 4564 KB Output is correct