| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 877988 | fanwen | Safety (NOI18_safety) | C++17 | 159 ms | 196192 KiB | 
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>
using namespace std;
#define fi first
#define se second
#define ll long long
#define file(name)                  \
    if(fopen(name".inp", "r"))      \
        freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout); \
const int MAX = 2e5 + 5;
int N, H, A[MAX];
namespace sub1 {
	bool check() {
		return N <= 5000 && *max_element(A + 1, A + N + 1) <= 5000;
	}
	long long dp[5000][5000];
	void solve() {
		int MaxA = *max_element(A + 1, A + N + 1);
		memset(dp, 0x3f, sizeof dp);
		memset(dp[0], 0, sizeof dp[0]);
		for (int i = 1; i <= N; ++i) {
			deque <int> q;
			int j = 0;
			for (int h = 0; h <= MaxA; ++h) {
				int l = max(0, h - H), r = min(MaxA, h + H);
				while(j <= r) {
					while(!q.empty() && dp[i - 1][q.back()] >= dp[i - 1][j]) q.pop_back();
					q.push_back(j);
					j++;
				}
				while(!q.empty() && h - q.front() > H) q.pop_front();
				if(!q.empty()) dp[i][h] = dp[i - 1][q.front()] + abs(h - A[i]);
			}
		}
		cout << *min_element(dp[N], dp[N] + MaxA + 1); exit(0);
	}
}
void you_make_it(void) {
    cin >> N >> H;
    for (int i = 1; i <= N; ++i) cin >> A[i];
    if(sub1::check()) sub1::solve();
}
signed main() {
#ifdef LOCAL
    freopen("TASK.inp", "r", stdin);
    freopen("TASK.out", "w", stdout);
#endif
    file("SAFETY");
    auto start_time = chrono::steady_clock::now();
    cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
    you_make_it();
    auto end_time = chrono::steady_clock::now();
    cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;
    return (0 ^ 0);
}
// Dream it. Wish it. Do it.
Compilation message (stderr)
| # | 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... | ||||
