Submission #1309584

#TimeUsernameProblemLanguageResultExecution timeMemory
1309584thecrazycandySafety (NOI18_safety)C++20
0 / 100
412 ms98336 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 = INFL;
    for (int i = 1; i <= 5000; i++) {
        for (int j = 0; j <= 5000; j++) dp[i][j] = INFL;
    }
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) {
        deque <pair <ll, ll>> dq;
        for (int j = 0; j < h; 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 + h; 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});
            }
            dp[i][j] = min(dp[i][j - h], 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...