#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
struct segtree{
vector<ll> tree;
segtree(int n){
tree.resize(4*n,1e18);
}
ll query(int l,int r,int ql,int qr,int pos){
if (ql > r or qr < l){return 1e18;}
if (ql<=l and qr >= r){return tree[pos];}
int mid = l+(r-l)/2;
return min(query(l,mid,ql,qr,pos*2),query(mid+1,r,ql,qr,pos*2+1));
}
void update(int l,int r,int pos,int qpos,ll qval){
if (l==r){tree[pos]=qval;return;}
int mid = l+(r-l)/2;
if (qpos <= mid){update(l,mid,pos*2,qpos,qval);}
else{update(mid+1,r,pos*2+1,qpos,qval);}
tree[pos]=min(tree[pos*2],tree[pos*2+1]);
}
};
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
int n,h;cin >> n >> h;
int k =10; // chnage to maxbound of arr[i]
vector<int> arr(n+1);for (int i = 1;i<=n;i++){cin >> arr[i];}
segtree dp(k+1);
for (int i = 0;i<=k;i++){dp.update(0,k,1,i,abs(arr[1]-i));}
for (int i = 2;i<=n;i++){
segtree ndp(k+1);
for (int p = 0;p<=k;p++){
ndp.update(0,k,1,p,dp.query(0,k,max(0,p-h),min(k,p+h),1)+abs(arr[i]-p));
}
dp = ndp;
}
cout << dp.query(0,k,0,k,1) << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |