Submission #978673

# Submission time Handle Problem Language Result Execution time Memory
978673 2024-05-09T13:13:21 Z nnin Palembang Bridges (APIO15_bridge) C++17
100 / 100
77 ms 8148 KB
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
using ll = long long;
#define f first
#define s second
 
vector<pii> cross;
int N, K;
 
ll check1(int bridge) {
    ll ret = 0;
    for(auto [st, en]:cross) {
        if(bridge<st) ret += (ll)st-bridge+en-bridge;
        else if(bridge>en) ret += (ll)bridge-st+bridge-en;
        else ret += (ll)en-st;
    }
    return ret;
}
 
ll one() {
    vector<int> v;
    for(auto [st, en]:cross) {
        v.push_back(st);
        v.push_back(en);
    }
    sort(v.begin(), v.end());
    return min(check1(v[((int)v.size()-1)/2]), check1(v[(int)v.size()/2]));
}

ll val1[100005], val2[100005], suml, sumr;
priority_queue<int> l;
priority_queue<int, vector<int>, greater<int>> r;

void insert(int x) {
    if(l.empty() || x<=l.top()) l.push(x), suml += x;
    else r.push(x), sumr += x;
    if(l.size()>r.size()+1) {
        sumr += l.top();
        suml -= l.top();
        r.push(l.top());
        l.pop();
    }
    if(r.size()>l.size()) {
        suml += r.top();
        sumr -= r.top();
        l.push(r.top());
        r.pop();
    }
}

bool cmp(pii a, pii b) {
    return (a.f+a.s)<(b.f+b.s);
}
 
ll two() {
    sort(cross.begin(), cross.end(), cmp);
    for(int i=0;i<cross.size()-1;i++) {
        insert(cross[i].f); insert(cross[i].s);
        val1[i] = sumr-suml;
    }
    while(!l.empty()) l.pop();
    while(!r.empty()) r.pop();
    suml = sumr = 0;
    for(int i=cross.size()-2;i>=0;i--) {
        insert(cross[i+1].f); insert(cross[i+1].s);
        val2[i] = sumr-suml;
    }
    ll ret = one();
    for(int i=0;i<cross.size()-1;i++) {
        ret = min(ret, val1[i]+val2[i]);
    }
    return ret;
}
 
int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    cin>>K>>N;
    ll ans = 0;
    for(int i=0;i<N;i++) {
        char z1, z2;
        int b1, b2;
        cin>>z1>>b1>>z2>>b2;
        if(b1>b2) swap(b1, b2);
        if(z1==z2) {
            ans += b2-b1;
        } else {
            cross.push_back({b1, b2});
        }
    }
    if(cross.empty()) {
        cout<<ans;
        return 0;
    }
    ans += cross.size();
    if(K==1) cout<<ans+one();
    else cout<<ans+two();
}

Compilation message

bridge.cpp: In function 'll two()':
bridge.cpp:58:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |     for(int i=0;i<cross.size()-1;i++) {
      |                 ~^~~~~~~~~~~~~~~
bridge.cpp:70:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for(int i=0;i<cross.size()-1;i++) {
      |                 ~^~~~~~~~~~~~~~~
# 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 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 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 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 16 ms 2632 KB Output is correct
13 Correct 32 ms 2776 KB Output is correct
14 Correct 23 ms 2520 KB Output is correct
15 Correct 19 ms 1540 KB Output is correct
16 Correct 19 ms 2612 KB Output is correct
17 Correct 21 ms 2776 KB Output is correct
18 Correct 25 ms 2776 KB Output is correct
19 Correct 31 ms 2776 KB Output is correct
20 Correct 21 ms 2776 KB Output is correct
21 Correct 28 ms 2776 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 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 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 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 444 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 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 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 344 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 468 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 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 464 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 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 1 ms 344 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 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 31 ms 6360 KB Output is correct
26 Correct 54 ms 6540 KB Output is correct
27 Correct 75 ms 7372 KB Output is correct
28 Correct 77 ms 7792 KB Output is correct
29 Correct 77 ms 8148 KB Output is correct
30 Correct 40 ms 4060 KB Output is correct
31 Correct 30 ms 6860 KB Output is correct
32 Correct 57 ms 7376 KB Output is correct
33 Correct 44 ms 7108 KB Output is correct
34 Correct 64 ms 7436 KB Output is correct
35 Correct 41 ms 7160 KB Output is correct
36 Correct 61 ms 7368 KB Output is correct
37 Correct 27 ms 6096 KB Output is correct