#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];
int dp[maxn][maxn];
int p[maxl][maxn];
int lg[maxn];
void test(){
cin >> n >> h;
for(int i = 2; i <= 5000; i++){
lg[i] = lg[i >> 1] + 1;
}
for(int i = 1; i <= n ;i++){
cin >> a[i];
}
for(int x = 0; x <= 5000; x++){
dp[1][x] = abs(x - a[1]);
}
for(int i = 2; i <= n; i++){
for(int x = 0; x <= 5000; x++){
p[0][x] = dp[i-1][x];
}
for(int k = 1; (1<<k) - 1 <= 5000; k++){
for(int x = 0; x + (1<<k) - 1 <= 5000; x++){
p[k][x] = min(p[k-1][x], p[k-1][x+(1<<(k-1))]);
}
}
for(int x = 0; x <= 5000; x++){
int l = max(0, x - h);
int r = min(5000, 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2660 KB |
Output is correct |
4 |
Incorrect |
1 ms |
2696 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1230 ms |
207592 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2660 KB |
Output is correct |
4 |
Incorrect |
1 ms |
2696 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2660 KB |
Output is correct |
4 |
Incorrect |
1 ms |
2696 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2660 KB |
Output is correct |
4 |
Incorrect |
1 ms |
2696 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2660 KB |
Output is correct |
4 |
Incorrect |
1 ms |
2696 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2660 KB |
Output is correct |
4 |
Incorrect |
1 ms |
2696 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |