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>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ent "\n"
const int maxn = 5e3 + 100;
const ll INF = (ll)1e18 + 100;
const int inf = 1e9 + 100;
const int MOD = 1e9 + 7;
const int maxl = 14;
int n, h;
int a[maxn];
ll dp[maxn][maxn];
ll p[maxl][maxn];
int lg[maxn];
void test(){
cin >> n >> h;
for(int i = 2; i <= 5001; i++){
lg[i] = lg[i >> 1] + 1;
}
for(int i = 1; i <= n ;i++){
cin >> a[i];
}
for(int x = 0; x <= 400; x++){
dp[1][x] = abs(x - a[1]);
}
for(int i = 2; i <= n; i++){
for(int x = 0; x <= 400; x++){
p[0][x] = dp[i-1][x];
}
for(int k = 1; (1<<k) - 1 <= 400; k++){
for(int x = 0; x + (1<<k) - 1 <= 400; x++){
p[k][x] = min(p[k-1][x], p[k-1][x+(1<<(k-1))]);
}
}
for(int x = 0; x <= 400; x++){
int l = max(0, x - h);
int r = min(400, x + h);
int k = lg[r - l + 1];
dp[i][x] = min(p[k][l], p[k][r-(1<<k)+1]);
dp[i][x] += abs(x - a[i]);
}
}
sort(dp[n], dp[n] + 5000 + 1);
cout << dp[n][0];
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t; t = 1;
while(t--) test();
cout << ent;
}
# | 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... |