Submission #886268

# Submission time Handle Problem Language Result Execution time Memory
886268 2023-12-11T16:23:01 Z tsumondai Palembang Bridges (APIO15_bridge) C++14
100 / 100
59 ms 6328 KB

#include <bits/stdc++.h>
using namespace std;
 
#define MAXN 100005
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
typedef long long lld;
 
int K, N, M;
lld D[MAXN], E[MAXN];
 
struct Z{
    int a, b;
} A[MAXN];
 
void proc(lld V[])
{
    if (!N) return;
    lld val = abs(A[1].a - A[1].b);
    priority_queue <int> p, q;
    int mid = A[1].a;
    if (A[1].b < mid) p.push(A[1].b);
    else q.push(-A[1].b);
    V[1] = val;
    for (int i=2;i<=N;i++){
        val += abs(mid - A[i].a) + abs(mid - A[i].b);
        if (A[i].a < mid) p.push(A[i].a);
        else q.push(-A[i].a);
        if (A[i].b < mid) p.push(A[i].b);
        else q.push(-A[i].b);
        while (sz(p) < sz(q)){
            int nxt = -q.top(); q.pop();
            p.push(mid);
            val += (lld)(nxt - mid) * sz(p) - (lld)(nxt - mid) * (sz(q) + 1);
            mid = nxt;
        }
        while (sz(p) > sz(q)+1){
            int nxt = p.top(); p.pop();
            q.push(-mid);
            val += (lld)(mid - nxt) * sz(q) - (lld)(mid - nxt) * (sz(p) + 1);
            mid = nxt;
        }
        V[i] = val;
    }
}
 
int main()
{
    scanf("%d%d", &K, &M);
    vector <int> arr;
    lld def = 0;
    while (M--){
        char p, q; int a, b;
        scanf(" %c%d %c%d", &p, &a, &q, &b);
        if (p == q){ def += abs(a-b); continue; }
        A[++N] = {a, b};
    }
    sort(A+1, A+N+1, [](const Z &a, const Z &b){
        return a.a+a.b < b.a+b.b;
    });
    proc(D);
    if (K == 1){ printf("%lld\n", D[N] + def + N); return 0; }
    reverse(A+1, A+N+1);
    proc(E);
    lld ans = 1e18;
    for (int i=0;i<=N;i++) ans = min(ans, def + D[i] + E[N-i] + N);
    printf("%lld\n", ans);
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:52:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |     scanf("%d%d", &K, &M);
      |     ~~~~~^~~~~~~~~~~~~~~~
bridge.cpp:57:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |         scanf(" %c%d %c%d", &p, &a, &q, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 25 ms 3556 KB Output is correct
13 Correct 38 ms 3608 KB Output is correct
14 Correct 36 ms 3548 KB Output is correct
15 Correct 22 ms 3288 KB Output is correct
16 Correct 22 ms 3792 KB Output is correct
17 Correct 32 ms 3652 KB Output is correct
18 Correct 23 ms 3548 KB Output is correct
19 Correct 37 ms 3532 KB Output is correct
20 Correct 28 ms 3780 KB Output is correct
21 Correct 35 ms 3536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 0 ms 2392 KB Output is correct
8 Correct 1 ms 2648 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 444 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2392 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2508 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2500 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 1 ms 2508 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 26 ms 4792 KB Output is correct
26 Correct 43 ms 4904 KB Output is correct
27 Correct 47 ms 5748 KB Output is correct
28 Correct 49 ms 6212 KB Output is correct
29 Correct 49 ms 6228 KB Output is correct
30 Correct 26 ms 4476 KB Output is correct
31 Correct 26 ms 5900 KB Output is correct
32 Correct 45 ms 6328 KB Output is correct
33 Correct 27 ms 5812 KB Output is correct
34 Correct 59 ms 6184 KB Output is correct
35 Correct 36 ms 6176 KB Output is correct
36 Correct 50 ms 5924 KB Output is correct
37 Correct 23 ms 4804 KB Output is correct