Submission #146176

#TimeUsernameProblemLanguageResultExecution timeMemory
146176mhy908Palembang Bridges (APIO15_bridge)C++14
100 / 100
135 ms10724 KiB
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
LL temp, ans=987654321987654321;
int n, re, k;
LL arr[200010], sum[200010], arrr[200010], arrrr[200010];
int re2;
pll input[100010];
LL lef, rig, mid;
priority_queue<LL> l, r;
void in_pq(LL a)
{
    if(mid<=a){
        r.push(-a);
        rig+=a;
    }
    else{
        l.push(a);
        lef+=a;
    }
    if(r.size()>=l.size()+2){
        LL temp=-r.top();
        r.pop();
        rig-=temp;
        l.push(mid);
        lef+=mid;
        mid=temp;
    }
    if(r.size()+2<=l.size()){
        LL temp=l.top();
        l.pop();
        lef-=temp;
        r.push(-mid);
        rig+=mid;
        mid=temp;
    }
}
LL solve()
{
    return mid*l.size()-lef+rig-mid*r.size();
}
int main()
{
    scanf("%d %d", &k, &n);
    for(int i=1; i<=n; i++){
        char a, b;
        LL c, d;
        scanf(" %c %lld %c %lld", &a, &c, &b, &d);
        if(a==b)temp+=llabs(c-d);
        else{
            arr[++re]=c;
            arr[++re]=d;
            input[++re2]={min(c, d), max(c, d)};
        }
    }
    sort(arr+1, arr+re+1);
    sort(input+1, input+re2+1, [](const pll& a, const pll& b){return a.F+a.S<b.F+b.S;});
    for(int i=1; i<=re; i++){
        sum[i]=sum[i-1]+arr[i];
    }
    if(k==1){
        ans=sum[re]-sum[re/2]*2;
        printf("%lld", ans+temp+(LL)re/2);
        return 0;
    }
    mid=input[1].F;
    in_pq(input[1].S);
    arrr[1]=solve();
    for(int i=2; i<=re2; i++){
        in_pq(input[i].F);
        in_pq(input[i].S);
        arrr[i]=solve();
    }
    lef=0;
    rig=0;
    while(!l.empty())l.pop();
    while(!r.empty())r.pop();
    mid=input[re2].F;
    in_pq(input[re2].S);
    arrrr[re2]=solve();
    for(int i=re2-1; i>=1; i--){
        in_pq(input[i].F);
        in_pq(input[i].S);
        arrrr[i]+=solve();
    }
    for(int i=0; i<=re2; i++){
        ans=min(ans, arrr[i]+arrrr[i+1]);
    }
    printf("%lld", ans+temp+(LL)re/2);
}

Compilation message (stderr)

bridge.cpp: In function 'int main()':
bridge.cpp:49:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &k, &n);
     ~~~~~^~~~~~~~~~~~~~~~~
bridge.cpp:53:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf(" %c %lld %c %lld", &a, &c, &b, &d);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...