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>
#define ll long long
#define fi first
#define se second
using namespace std;
const int nmax = 200002;
int S[nmax];
int dp[5005][5005];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int N;
int H;
cin >> N >> H;
for(int i=1; i<=N; i++) {
cin >> S[i];
}
deque<pair<int,int>>dd;
for(int i=0; i<=5000; i++) dp[1][i] = abs(S[1]-i);
for(int i=2; i<=N; i++) {
dd.clear();
for(int j=0; j<=min(5000, H); j++) {
while(!dd.empty() && dd.back().fi > dp[i-1][j]) {
dd.pop_back();
}
dd.push_back({dp[i-1][j],j});
}
pair<int,int>sem;
for(int j=0; j<=5000; j++) {
if(!dd.empty() && dd.front().se < j-H) {
dd.pop_front();
}
sem = dd.front();
dp[i][j] = sem.fi + abs(S[i]-j);
if(j-H+1 <= 5000) {
while(!dd.empty() && dd.back().fi > dp[i-1][j+H+1]) {
dd.pop_back();
}
dd.push_back({dp[i-1][j+H+1], j+H+1});
}
}
}
int jwb = INT_MAX;
for(int i=0; i<=5000; i++) {
jwb = min(jwb, dp[N][i]);
}
cout << jwb << endl;
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... |