Submission #243306

#TimeUsernameProblemLanguageResultExecution timeMemory
243306Leonardo_PaesPalembang Bridges (APIO15_bridge)C++17
63 / 100
2092 ms8148 KiB
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long ll;
const ll inf = 1e18+10;
typedef pair<ll,ll> pii;
int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    int k, n;
    cin >> k >> n;
    ll ans1 = 0, ans2 = 0;
    vector<ll> a, b, c;
    for(int i=1; i<=n; i++){
        int s, t;
        char p, q;
        cin >> p >> s >> q >> t;
        if(p == q) ans1 += abs(s - t);
        else{
            if(p == 'B') swap(s, t);
            if(s > t) swap(s, t);
            a.push_back(s);
            b.push_back(t);
        }
    }
    for(auto x : a) c.push_back(x);
    for(auto x : b) c.push_back(x);
    if(k == 1){
        for(auto x : a) c.push_back(x);
        for(auto x : b) c.push_back(x);
        sort(c.begin(), c.end());
        if(!c.empty()){
            ll opt = c[(int)c.size()/2];
            for(auto x : a) ans2 += abs(opt - x);
            for(auto x : b) ans2 += abs(opt - x);
        }
    }
    else if(!c.empty()){
        ans2 = inf;
        for(int i=0; i<c.size(); i++){
            priority_queue<pii, vector<pii>, greater<pii>> fila;
            ll g = 0, delta = 0;
            for(int j=0; j<a.size(); j++){
                if(a[j] <= c[i] and c[i] <= b[j]) g += b[j] - a[j];
                else{
                    ll gap = min(abs(a[j] - c[i]), abs(b[j] - c[i]));
                    g += 2*gap+b[j]-a[j];
                    fila.push({a[j], +2});
                    fila.push({b[j], +2});
                    fila.push({a[j] - gap, -2});
                    fila.push({b[j] + gap, -2});
                }
            }
            ll x = -inf;
            ans2 = min(ans2, g);
            while(!fila.empty()){
                pii event = fila.top();
                fila.pop();
                g += delta*(event.f - x);
                ans2 = min(ans2, g);
                x = event.f;
                delta += event.s;
            }
        }
    }
    cout << ans1 + ans2 + (int)a.size() << "\n";
    return 0;
}

Compilation message (stderr)

bridge.cpp: In function 'int main()':
bridge.cpp:40:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0; i<c.size(); i++){
                      ~^~~~~~~~~
bridge.cpp:43:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int j=0; j<a.size(); j++){
                          ~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...