Submission #146176

# Submission time Handle Problem Language Result Execution time Memory
146176 2019-08-22T15:33:03 Z mhy908 Palembang Bridges (APIO15_bridge) C++14
100 / 100
135 ms 10724 KB
#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

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 time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 1 ms 380 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 36 ms 4984 KB Output is correct
13 Correct 75 ms 4984 KB Output is correct
14 Correct 57 ms 4572 KB Output is correct
15 Correct 44 ms 3064 KB Output is correct
16 Correct 41 ms 5112 KB Output is correct
17 Correct 53 ms 4984 KB Output is correct
18 Correct 53 ms 4984 KB Output is correct
19 Correct 73 ms 4956 KB Output is correct
20 Correct 45 ms 4984 KB Output is correct
21 Correct 64 ms 5020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 1 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 400 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 3 ms 504 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 3 ms 504 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 3 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 3 ms 504 KB Output is correct
20 Correct 3 ms 376 KB Output is correct
21 Correct 3 ms 376 KB Output is correct
22 Correct 3 ms 376 KB Output is correct
23 Correct 3 ms 504 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 3 ms 376 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 3 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 3 ms 504 KB Output is correct
20 Correct 3 ms 504 KB Output is correct
21 Correct 3 ms 504 KB Output is correct
22 Correct 3 ms 504 KB Output is correct
23 Correct 3 ms 504 KB Output is correct
24 Correct 3 ms 504 KB Output is correct
25 Correct 59 ms 9188 KB Output is correct
26 Correct 93 ms 9340 KB Output is correct
27 Correct 129 ms 10172 KB Output is correct
28 Correct 132 ms 10720 KB Output is correct
29 Correct 135 ms 10720 KB Output is correct
30 Correct 68 ms 5868 KB Output is correct
31 Correct 70 ms 10080 KB Output is correct
32 Correct 99 ms 10724 KB Output is correct
33 Correct 82 ms 10316 KB Output is correct
34 Correct 120 ms 10724 KB Output is correct
35 Correct 73 ms 10212 KB Output is correct
36 Correct 110 ms 10464 KB Output is correct
37 Correct 54 ms 9312 KB Output is correct