This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
//#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e3 + 5;
int s[N];
ll dp[N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, h;
cin >> n >> h;
vector<int> a;
for (int i = 0; i < n; i++) {
cin >> s[i];
a.push_back(max(s[i] - h, 0));
a.push_back(s[i]);
a.push_back(s[i] + h);
}
sort(a.begin(), a.end());
a.resize(a.end() - unique(a.begin(), a.end()));
int m = a.size();
for (int j = 0; j < m; j++) {
dp[0][j] = abs(s[0] - j);
}
for (int i = 1; i < n; i++) {
int l = 0, r = 0;
multiset<ll> q;
for (int j = 0; j < m; j++) {
while (r < m && a[r] <= j + h) {
q.insert(dp[i - 1][r]);
r++;
}
while (l < m && a[l] < j - h) {
q.erase(q.find(dp[i - 1][l]));
l++;
}
dp[i][j] = abs(s[i] - a[j]) + *q.begin();
}
}
ll ans = LLONG_MAX;
for (int i = 0; i < m; i++) {
ans = min(ans, dp[n - 1][i]);
}
cout << ans;
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... |