Submission #1309592

#TimeUsernameProblemLanguageResultExecution timeMemory
1309592thecrazycandySafety (NOI18_safety)C++20
35 / 100
265 ms98340 KiB
#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
//#pragma GCC target("avx,avx2,fma")
using namespace std;
#define ll int
#define sped_up ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define rall(v) (v).rbegin(), (v).rend()
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define S second
#define F first
const ll INF = (ll)1e9 + 1, INFL = (ll)1e18 + 1;
const ll mod = (ll)1e9 + 7, MAXN = (ll)5001;
ll dp[MAXN][MAXN];
ll a[MAXN], n, h;
int main() {
    sped_up;
    cin >> n >> h;
    ll mn = INF;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        for (int j = 0; j <= 5000; j++) dp[i][j] = INF;
    }
    for (int i = 1; i <= n; i++) {
        deque <pair <ll, ll>> dq;
        for (int j = 0; j < min(h, 5000); j++) {
            while (!dq.empty() && dq.back().F > dp[i - 1][j]) dq.pop_back();
            dq.push_back({dp[i - 1][j], j});
        }
        for (int j = 0; j <= 5000; j++) {
            while (!dq.empty() && abs(dq.front().S - j) > h) dq.pop_front();
            if (j + h <= 5000) {
                while (!dq.empty() && dq.back().F > dp[i - 1][j + h]) dq.pop_back();
                dq.push_back({dp[i - 1][j + h], j + h});
            }
            dp[i][j] = min(dp[i][j], dq.front().F + abs(a[i] - j));
        }
    }
    for (int i = 0; i <= 5000; i++) mn = min(mn, dp[n][i]);
    cout << mn;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...