Submission #764200

# Submission time Handle Problem Language Result Execution time Memory
764200 2023-06-23T08:33:02 Z qwerasdfzxcl Palembang Bridges (APIO15_bridge) C++17
100 / 100
50 ms 4232 KB
#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
using namespace std;

typedef long long ll;
const int mx = 100005;

int k, n, m;
ll D[mx], E[mx];

struct People{
    int a, b;
    bool operator < (People z){
        return a + b < z.a + z.b;
    }
} P[mx];

void subpr(ll *V){
    if(!n) return;
    ll dval = abs(P[0].a - P[0].b);
    priority_queue<int> lft;
    priority_queue<int,vector<int>,greater<int>> rgt;

    int brp = P[0].a;
    (P[0].b < brp ? lft.push(P[0].b) : rgt.push(P[0].b));
    V[0] = dval;
    for(int i = 1; i < n; i++){
        dval += abs(brp - P[i].a) + abs(brp - P[i].b);
        P[i].a < brp ? lft.push(P[i].a) : rgt.push(P[i].a);
        P[i].b < brp ? lft.push(P[i].b) : rgt.push(P[i].b);
        while(sz(lft) < sz(rgt)){
            int nbr = rgt.top(); rgt.pop();
            lft.push(brp);
            dval += (ll)(nbr - brp) * (sz(lft) - sz(rgt) - 1);
            brp = nbr;
        }
        while(sz(lft) > sz(rgt) + 1){
            int nbr = lft.top(); lft.pop();
            rgt.push(brp);
            dval += (ll)(brp - nbr) * (sz(rgt) - sz(lft) - 1);
            brp = nbr;
        }
        V[i] = dval;
    }
}

ll off;
void input(){
    scanf("%d %d",&k,&m);
    while(m--){
        char p,q;
        int a,b;
        scanf("%*c%c %d %c %d",&p,&a,&q,&b);
        if(p == q) off += abs(a - b);
        else P[n++] = {a, b};
    }
}

int main(){
    input();
    sort(P,P+n);
    subpr(D);
    if(k == 1) return !printf("%lld\n",D[n-1] + off + n);
    reverse(P,P+n);
    subpr(E);
    ll ans = min(D[n-1], E[n-1]);
    for(int i = 0; i < n - 1; i++){
        ans = min(ans, D[i] + E[n - 2 - i]);
    }
    printf("%lld\n",ans + off + n);
}

Compilation message

bridge.cpp: In function 'void input()':
bridge.cpp:49:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |     scanf("%d %d",&k,&m);
      |     ~~~~~^~~~~~~~~~~~~~~
bridge.cpp:53:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |         scanf("%*c%c %d %c %d",&p,&a,&q,&b);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 24 ms 2748 KB Output is correct
13 Correct 38 ms 2672 KB Output is correct
14 Correct 31 ms 2492 KB Output is correct
15 Correct 23 ms 1820 KB Output is correct
16 Correct 24 ms 2816 KB Output is correct
17 Correct 33 ms 2820 KB Output is correct
18 Correct 24 ms 2756 KB Output is correct
19 Correct 37 ms 2840 KB Output is correct
20 Correct 29 ms 2752 KB Output is correct
21 Correct 40 ms 2792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 27 ms 3824 KB Output is correct
26 Correct 39 ms 3812 KB Output is correct
27 Correct 48 ms 3808 KB Output is correct
28 Correct 49 ms 3784 KB Output is correct
29 Correct 50 ms 3768 KB Output is correct
30 Correct 25 ms 2324 KB Output is correct
31 Correct 25 ms 3772 KB Output is correct
32 Correct 42 ms 3792 KB Output is correct
33 Correct 27 ms 3888 KB Output is correct
34 Correct 48 ms 3816 KB Output is correct
35 Correct 34 ms 4232 KB Output is correct
36 Correct 47 ms 3872 KB Output is correct
37 Correct 24 ms 3896 KB Output is correct