Submission #1288647

#TimeUsernameProblemLanguageResultExecution timeMemory
1288647yonatanlSafety (NOI18_safety)C++20
0 / 100
10 ms1536 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <math.h>

#define rep(i, s, e) for (ll i = s; i < e; i++)
#define upmax(a, b) a = max(a, b)
#define upmin(a, b) a = min(a, b)

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

//const ll MAX_H = 5005;

void solve() {
	ll n, H;
	cin >> n >> H;
	vll arr(n);
	rep(i, 0, n) cin >> arr[i];
	ll MAX_H = 2 * (*(max_element(arr.begin(), arr.end())));
	vll dp(MAX_H, INF);
	rep(j, 0, MAX_H) {
		dp[j] = abs(arr[0] - j);
	}
	rep(i, 1, n) {
		vll nextdp(MAX_H, INF);
		deque<pll> dq;
		rep(k, 0, min(MAX_H, H + 1)) {
			while (!dq.empty() && dq.back().first > dp[k]) dq.pop_back();
			dq.push_back({ dp[k], k });
		}
		/*multiset<ll> s;
		rep(k, 0, min(MAX_H, H + 1)) {
			s.insert(dp[k]);
		}
		nextdp[0] = *s.begin() + abs(arr[i]);*/
		rep(j, 1, MAX_H) {
			if (j + H < MAX_H) {
				while (!dq.empty() && dq.back().first > dp[j + H]) dq.pop_back();
				dq.push_back({ dp[j + H], j + H });
			}
			nextdp[j] = dq.front().first + abs(arr[i] - j);
			if (dq.front().second <= j - H) {
				dq.pop_front();
			}
			/*if (j + H < MAX_H) {
				s.insert(dp[j + H]);
			}
			nextdp[j] = *(s.begin()) + abs(arr[i] - j);
			if (j - H >= 0) {
				auto it = s.find(dp[j - H]);
				s.erase(it);
			}*/
		}
		swap(nextdp, dp);
	}
	ll res = INF;
	rep(j, 0, MAX_H) upmin(res, dp[j]);
	cout << res << endl;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	solve();
}
#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...