Submission #746228

# Submission time Handle Problem Language Result Execution time Memory
746228 2023-05-22T02:42:01 Z Seb Safety (NOI18_safety) C++17
100 / 100
78 ms 5476 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct F{
    ll m,b,izq,der;
    priority_queue <ll> pq1;
    priority_queue <ll,vector<ll>,greater<ll>> pq2;
    F(ll M, ll B, vector <ll> V) {
        m = M;
        b = B;
        izq = 0;
        der = 0;
        if (!V.empty()) {
            pq1.push(V[0]);
            pq2.push(V[1]);
        }
    }
    void unir(F a) {
        m += a.m;
        b += a.b;
        if (pq1.top()+izq >= a.pq1.top()) {
            while (!a.pq1.empty()) {
                pq1.push(a.pq1.top()-izq);
                a.pq1.pop();
            }
            while (!a.pq2.empty()) {
                pq1.push(a.pq2.top()-izq);
                a.pq2.pop();
            }
        }
        else if (pq2.top()+der <= a.pq2.top()) {
            while (!a.pq1.empty()) {
                pq2.push(a.pq1.top()-der);
                a.pq1.pop();
            }
            while (!a.pq2.empty()) {
                pq2.push(a.pq2.top()-der);
                a.pq2.pop();
            }
        }
        else {
            while (!a.pq1.empty()) {
                pq1.push(a.pq1.top()-izq);
                a.pq1.pop();
            }
            while (!a.pq2.empty()) {
                pq2.push(a.pq2.top()-der);
                a.pq2.pop();
            }
        }
        return;
    }
    void act(ll h) {
        if (m==1) {
            m--;
            b += (pq1.top()+izq);
            pq2.push(pq1.top()+izq-der);
            pq1.pop();
        }
        else if (m==-1) {
            m++;
            b -= (pq2.top()+der);
            pq1.push(pq2.top()+der-izq);
            pq2.pop();
        }
        izq -= h;
        der += h;
        return;
    }
};

int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
ll n,h,aux;
cin>>n>>h;
F f = F(0,0,{});
for (int i=0;i<n;i++) {
    cin>>aux;
    if (!f.pq1.empty() && !f.pq2.empty()) {
        if (f.pq1.top()+f.izq >= aux) f.unir(F(1,-aux,{aux,aux}));
        else if (f.pq2.top()+f.der <= aux) f.unir(F(-1,aux,{aux,aux}));
        else f.unir(F(0,0,{aux,aux}));
    }
    else f = F(0,0,{aux,aux});
    f.act(h);
}
cout<<f.b;
return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 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 392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 4744 KB Output is correct
2 Correct 78 ms 5136 KB Output is correct
3 Correct 70 ms 5360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 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 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 1 ms 392 KB Output is correct
13 Correct 1 ms 312 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 340 KB Output is correct
17 Correct 1 ms 320 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 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 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 1 ms 392 KB Output is correct
13 Correct 1 ms 312 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 340 KB Output is correct
17 Correct 1 ms 320 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 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 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 1 ms 392 KB Output is correct
13 Correct 1 ms 312 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 340 KB Output is correct
17 Correct 1 ms 320 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 464 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
31 Correct 1 ms 328 KB Output is correct
32 Correct 2 ms 480 KB Output is correct
33 Correct 2 ms 460 KB Output is correct
34 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 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 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 1 ms 392 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 312 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 320 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 2 ms 464 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 1 ms 328 KB Output is correct
38 Correct 2 ms 480 KB Output is correct
39 Correct 2 ms 460 KB Output is correct
40 Correct 2 ms 468 KB Output is correct
41 Correct 2 ms 468 KB Output is correct
42 Correct 2 ms 340 KB Output is correct
43 Correct 2 ms 528 KB Output is correct
44 Correct 2 ms 532 KB Output is correct
45 Correct 2 ms 480 KB Output is correct
46 Correct 2 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 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 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 1 ms 392 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 45 ms 4744 KB Output is correct
20 Correct 78 ms 5136 KB Output is correct
21 Correct 70 ms 5360 KB Output is correct
22 Correct 1 ms 312 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 320 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 2 ms 464 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 2 ms 468 KB Output is correct
40 Correct 1 ms 328 KB Output is correct
41 Correct 2 ms 480 KB Output is correct
42 Correct 2 ms 460 KB Output is correct
43 Correct 2 ms 468 KB Output is correct
44 Correct 2 ms 468 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 528 KB Output is correct
47 Correct 2 ms 532 KB Output is correct
48 Correct 2 ms 480 KB Output is correct
49 Correct 2 ms 512 KB Output is correct
50 Correct 69 ms 5476 KB Output is correct
51 Correct 76 ms 5060 KB Output is correct
52 Correct 41 ms 5044 KB Output is correct
53 Correct 53 ms 5164 KB Output is correct
54 Correct 63 ms 5152 KB Output is correct
55 Correct 47 ms 5100 KB Output is correct
56 Correct 47 ms 5080 KB Output is correct
57 Correct 44 ms 5116 KB Output is correct
58 Correct 46 ms 4620 KB Output is correct
59 Correct 49 ms 5168 KB Output is correct