Submission #586465

#TimeUsernameProblemLanguageResultExecution timeMemory
586465NeroZeinPalembang Bridges (APIO15_bridge)C++14
0 / 100
2 ms524 KiB
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define FOR(i,a, b) for (int i=a; i<=(b); i++)
#define F0R(i,a, b) for (int i=a; i>=(b); i--)

using namespace std;

int n, k, kep;
long long ans, slo, sup, pref[100005];

bool cmp(pair<int, int> a, pair<int, int> b)
    { return a.F + a.S < b.F + b.S; }


vector<pair<int,int>> v;

priority_queue<int>lo;
priority_queue<int,vector<int>,greater<int>>hi;

void ins (int x){

    if (lo.empty()){lo.push(x);slo+=x;return;}

    if (x > lo.top())
        hi.push(x), sup+=x;
    else
        lo.push(x), slo+=x;

    if (hi.size() > lo.size()){
        int top = hi.top();
        hi.pop();
        lo.push(top);
        sup -= top;
        slo += top;
    }

    else if (lo.size() > hi.size()+1){
        int top = hi.top();
        lo.pop();
        hi.push(top);
        slo -= top;
        sup += top;
    }

}

int32_t main(){

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> k >> n;
    FOR(i,1,n){
        char c1, c2;
        int x, y;
        cin >> c1 >> x >> c2 >> y;
        if (c1 == c2)
            kep += abs(x-y);
        else
            v.pb({x,y});
    }

    n = v.size();
    sort(v.begin(),v.end(),cmp);
    FOR(i,0,n-1){
        ins(v[i].F);
        ins(v[i].S);
        pref[i] = sup-slo;
    }

    ans = pref[n-1];

    if (k == 2){

        while (hi.size())hi.pop(); sup=0;
        while (lo.size())lo.pop(); slo=0;

        F0R(i,n-1, 1){
            ins(v[i].F);
            ins(v[i].S);
            ans = min(ans, (long long)sup-slo+pref[i-1]);
        }

    }

    cout << ans + kep + n;
}

Compilation message (stderr)

bridge.cpp: In function 'int32_t main()':
bridge.cpp:77:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
   77 |         while (hi.size())hi.pop(); sup=0;
      |         ^~~~~
bridge.cpp:77:36: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
   77 |         while (hi.size())hi.pop(); sup=0;
      |                                    ^~~
bridge.cpp:78:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
   78 |         while (lo.size())lo.pop(); slo=0;
      |         ^~~~~
bridge.cpp:78:36: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
   78 |         while (lo.size())lo.pop(); slo=0;
      |                                    ^~~
#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...