#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#define getchar_unlocked _getchar_nolock
int n, h, S[300005];
int dp[5005][5005];
deque<pi>dq;
void solve(){
cin >> n >> h;
for(int i=1;i<=n;i++)cin >> S[i];
for(int i=0;i<=5000;i++)dp[1][i] = abs(S[1] - i);
for(int i=2;i<=n;i++){
dq.clear();
for(int j=0;j<=h;j++){
while(!dq.empty() && dq.back().fi > dp[i-1][j])dq.pop_back();
dq.push_back({dp[i-1][j], j});
}
for(int j=0;j<=5000;j++){
if(!dq.empty() && dq.front().se < j - h)dq.pop_front();
assert(!dq.empty());
pi tmp = dq.front();
dp[i][j] = tmp.fi + abs(S[i] - j);
if(j + h + 1 <= 5000){
while(!dq.empty() && dq.back().fi > dp[i-1][j + h + 1])dq.pop_back();
dq.push_back({dp[i-1][j+h+1], j+h+1});
}
}
}
int ans = 1e18;
for(int i=0;i<=5000;i++)ans = min(ans, dp[n][i]);
cout << ans;
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
while(tc--){
solve();
}
}
Compilation message
safety.cpp:41:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
41 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Runtime error |
279 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
569 ms |
262144 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Runtime error |
279 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Runtime error |
279 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Runtime error |
279 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Runtime error |
279 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Runtime error |
279 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |