#include <bits/stdc++.h>
using namespace std;
int k,n,q,i,j,b,u,L,R,g,e,f,jj;
long long m,z,xx,w;
priority_queue<array<int,2>> a;
vector<bool> v;
vector<int> h;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
m=1e9+7;
cin >> n >> k;
for(i=0;i<n;i++){
cin >> b;
a.push({b,i});
h.push_back(b);
v.push_back(0);
}
z=0;
while(!a.empty()) {
g=a.top()[0];
e=a.top()[1];
a.pop();
if(v[e]) continue;
v[e]=1;
if (e>0 and !v[e-1] and h[e-1]<g-k)
{
z+=g-k-h[e-1];
h[e-1]=g-k;
a.push({g-k,e-1});
}
if (e<n-1 and !v[e+1] and h[e+1]<g-k)
{
z+=g-k-h[e+1];
h[e+1]=g-k;
a.push({g-k,e+1});
}
}
cout << z << '\n';
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include <bits/stdc++.h> using namespace std; int k,n,q,i,j,b,u,L,R,g,e,f,jj; long long m,z,xx,w; priority_queue<array<int,2>> a; vector<bool> v; vector<int> h; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); m=1e9+7; cin >> n >> k; for(i=0;i<n;i++){ cin >> b; a.push({b,i}); h.push_back(b); v.push_back(0); } z=0; while(!a.empty()) { g=a.top()[0]; e=a.top()[1]; a.pop(); if(v[e]) continue; v[e]=1; if (e>0 and !v[e-1] and h[e-1]<g-k) { z+=g-k-h[e-1]; h[e-1]=g-k; a.push({g-k,e-1}); } if (e<n-1 and !v[e+1] and h[e+1]<g-k) { z+=g-k-h[e+1]; h[e+1]=g-k; a.push({g-k,e+1}); } } cout << z << '\n'; } |
English