Submission #1044873

#TimeUsernameProblemLanguageResultExecution timeMemory
1044873alex_2008Text editor (CEOI24_editor)C++14
45 / 100
991 ms39568 KiB
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
typedef long long ll;
using namespace std;
const int N = 2e5 + 10;
int l[N];
set <int> ms;
priority_queue <pair<ll, pair<int, int>>> Q;
map <pair<int, int>, ll> mp;
void check(pair<int, int> v, ll d) {
	if (mp.find(v) == mp.end() || mp[v] > d) {
		mp[v] = d;
		Q.push({ -d, v });
	}
}
int main() {
	int n;
	cin >> n;
	int sl, sc, el, ec;
	cin >> sl >> sc >> el >> ec;
	ms.insert(1);
	ms.insert(sc);
	ms.insert(ec);
	for (int i = 1; i <= n; i++) {
		cin >> l[i];
		l[i]++;
		ms.insert(l[i]);
	}
	mp[{ sl, sc }] = 0;
	Q.push({ 0, { sl, sc } });
	while (!Q.empty()) {
		pair<ll, pair<int, int>> v = Q.top(); Q.pop();
		//cout << v.first << " " << v.second.first << " " << v.second.second << "\n";
		ll d = -v.first;
		int r = v.second.first;
		int c = v.second.second;
		if (mp[{ r, c }] != d) continue;
		if (c == l[r]) {
			if (r != n) {
				check({ r + 1, 1 }, d + 1);
			}
		}
		else {
			auto it = ms.upper_bound(c);
			check({ r, *it }, *it - c + d);
		}
		if (c == 1) {
			if (r != 1) {
				check({ r - 1, l[r - 1] }, d + 1);
			}
		}
		else {
			auto it = ms.lower_bound(c); it--;
			check({ r, *it }, c - *it + d);
		}
		if (r > 1) {
			check({ r - 1, min(l[r - 1], c) }, d + 1);
		}
		if (r < n) {
			check({ r + 1, min(l[r + 1], c) }, d + 1);
		}
	}
	cout << mp[{ el, ec }] << "\n";
}
#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...