This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
vector <ll> a;
vector <ll> s(22222,LLONG_MAX);
void build(ll id = 1, ll l = 0, ll r = a.size()){
if (r - l < 2) {
s[id] = a[l];
return;
}
ll mid = (l+r)/2;
build (id*2,l,mid);
build (id*2+1,mid,r);
s[id] = min(s[id*2],s[id*2+1]);
}
ll query(ll x,ll y,ll id = 1, ll l = 0, ll r = a.size()){
if (y <= l || x >= r) return LLONG_MAX;
if (x <= l && r <= y) return s[id];
ll mid = (l+r)/2;
return min(query(x,y,id*2,l,mid),query(x,y,id*2+1,mid,r));
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n,d;
cin >> n >> d;
vector <vector<ll>> dp(n+1,vector<ll>(5555,0));
vector <ll> arr(n+1);
ll maxi = 0;
for (int z=1;z<=n;z++){
cin >> arr[z];
maxi = max(maxi,arr[z]);
}
for (int z=1;z<=n;z++){
a = dp[z-1];
build();
for (int h=0;h<=maxi;h++){
ll temp = query(h-d,min(maxi+1,h+d+1));
dp[z][h] = temp+abs(arr[z]-h);
}
}
ll mini = LLONG_MAX;
for(int z=0;z<=maxi;z++){
mini = min(dp[n][z],mini);
}
cout << mini << endl;
}
# | 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... |