Submission #418651

# Submission time Handle Problem Language Result Execution time Memory
418651 2021-06-05T16:45:00 Z Blagojce Safety (NOI18_safety) C++11
100 / 100
118 ms 6980 KB
#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
  
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
  
const int mxn = 2e5+5;
const ll inf = 1e18;
const int i_inf = 1e9;
const ll mod = 1000003;
mt19937 _rand(time(NULL));
clock_t z;
//while((clock()-z)/CLOCKS_PER_SEC > 0.9)
 
 
int n;
ll k;
     
ll a[mxn];/*
struct p{
    ll x, y;
    ll delta;
};
 
vector<p> v1;
vector<p> v2;
 
void Shift(ll h){
    fr(i, 0, (int)v1.size()){
        v1[i].x -= h;
    }
    fr(i, 0, (int)v2.size()){
        v2[i].x += h;
    }
}
 
void Insert(ll h){
    if(h <= v1.back().x){
        fr(i, 0, (int)v2.size()){
            ++v2[i].delta;
        }
         
        fr(i, 0, (int)v1.size()){
            if(h <= v1[i].x){
                ll yh = v1[i].y + abs(v1[i].x - h)*v1[i].delta;
                ll deltah = v1[i].delta+1;
                 
                fr(j, 0, i){
                    ++v1[j].delta;
                }
                fr(j, i, (int)v1.size()){
                    --v1[j].delta;
                }
                 
                p nov = v1.back();
                nov.delta = 1;
                v2.insert(v2.begin(), nov); 
                 
                if(v1.back().delta == 0) v1.pop_back();
                 
                nov.x = h;
                nov.y = yh;
                nov.delta = deltah;
                v1.insert(v1.begin()+i, nov);
                 
                break;
            }
        }
    }
    else if(h >= v2[0].x){
        fr(i, 0, (int)v1.size()){
            ++v1[i].delta;
        }
         
         
        for(int i = (int)v2.size()-1; i >= 0; i --){
            if(h >= v2[i].x){
                ll yh = v2[i].y + abs(v2[i].x-h)*v2[i].delta;
                ll deltah = v2[i].delta+1;
                 
                fr(j, 0, i+1){
                    --v2[j].delta;
                }
                fr(j, i+1, (int)v2.size()){
                    ++v2[j].delta;
                }
                 
                 
                 
                p nov;
                nov.x = h;
                nov.y = yh;
                nov.delta = deltah;
                v2.insert(v2.begin()+i+1, nov);
                 
                 
                nov = v2[0];
                nov.delta = 1;
                v1.pb(nov);
                if(v2[0].delta == 0) v2.erase(v2.begin());
                 
                break;
            }
        }
    }
    else{
        fr(i, 0, (int)v1.size()){
            v1[i].delta ++;
        }
        fr(i, 0, (int)v2.size()){
            v2[i].delta ++;
        }
         
        p nov;
        nov.y = v1.back().y;
        nov.x = h;
        nov.delta = 1;
        v1.pb(nov);
        v2.insert(v2.begin(), nov);
    }
     
     
    fr(i, 0, (int)v1.size()){
        v1[i].y += abs(v1[i].x-h);
    }
    fr(i, 0, (int)v2.size()){
        v2[i].y += abs(v2[i].x-h);
    }
     
}
*/
 
int main(){
    cin >> n >> k;
    fr(i, 0, n){
        cin >> a[i];
    }
     
    pq <ll> v1;
    pq <ll> v2;
    v1.push(a[0]);
    v2.push(-a[0]);
     
    ll ans = 0;
    fr(i, 1, n){
        ll vtop1 = v1.top();
        ll vtop2 = -v2.top();
         
        ll lef = vtop1 - i*k;
        ll rig = vtop2 + i*k;
        if(a[i] < lef){
            ans += abs(lef-a[i]);
            v1.push(a[i]+i*k);
            v1.push(a[i]+i*k);
            v1.pop();
            v2.push(-(lef-i*k));
        }
        else if(a[i] > rig){
            ans += abs(rig-a[i]);
            v2.push(-(a[i]-i*k));
            v2.push(-(a[i]-i*k));
            v2.pop();
            v1.push(rig+i*k);
        }
        else{
            v1.push(a[i]+i*k);
            v2.push(-(a[i]-i*k));
        }
     
    }
    cout<<ans<<endl;
     
    /*
     
    p nov;
    nov.x = a[0];
    nov.y = 0;
    nov.delta = 1;
    v1.pb(nov);
    v2.pb(nov);
    fr(i, 1, n){
        Shift(k);
        Insert(a[i]);
    }
    cout<<v1.back().y<<endl;*/
     
 
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 5720 KB Output is correct
2 Correct 115 ms 6744 KB Output is correct
3 Correct 117 ms 6872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 3 ms 460 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
31 Correct 2 ms 444 KB Output is correct
32 Correct 2 ms 460 KB Output is correct
33 Correct 2 ms 460 KB Output is correct
34 Correct 2 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 300 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 3 ms 460 KB Output is correct
35 Correct 2 ms 460 KB Output is correct
36 Correct 2 ms 460 KB Output is correct
37 Correct 2 ms 444 KB Output is correct
38 Correct 2 ms 460 KB Output is correct
39 Correct 2 ms 460 KB Output is correct
40 Correct 2 ms 460 KB Output is correct
41 Correct 4 ms 488 KB Output is correct
42 Correct 3 ms 444 KB Output is correct
43 Correct 3 ms 460 KB Output is correct
44 Correct 3 ms 460 KB Output is correct
45 Correct 3 ms 460 KB Output is correct
46 Correct 3 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 300 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 78 ms 5720 KB Output is correct
20 Correct 115 ms 6744 KB Output is correct
21 Correct 117 ms 6872 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 3 ms 460 KB Output is correct
38 Correct 2 ms 460 KB Output is correct
39 Correct 2 ms 460 KB Output is correct
40 Correct 2 ms 444 KB Output is correct
41 Correct 2 ms 460 KB Output is correct
42 Correct 2 ms 460 KB Output is correct
43 Correct 2 ms 460 KB Output is correct
44 Correct 4 ms 488 KB Output is correct
45 Correct 3 ms 444 KB Output is correct
46 Correct 3 ms 460 KB Output is correct
47 Correct 3 ms 460 KB Output is correct
48 Correct 3 ms 460 KB Output is correct
49 Correct 3 ms 460 KB Output is correct
50 Correct 118 ms 6980 KB Output is correct
51 Correct 100 ms 6408 KB Output is correct
52 Correct 87 ms 6448 KB Output is correct
53 Correct 97 ms 6768 KB Output is correct
54 Correct 112 ms 6712 KB Output is correct
55 Correct 87 ms 6424 KB Output is correct
56 Correct 93 ms 6624 KB Output is correct
57 Correct 89 ms 6396 KB Output is correct
58 Correct 66 ms 5548 KB Output is correct
59 Correct 104 ms 6608 KB Output is correct