Submission #1075178

#TimeUsernameProblemLanguageResultExecution timeMemory
1075178pawnedText editor (CEOI24_editor)C++17
5 / 100
1 ms348 KiB
#pragma GCC optimize("O1,O2,O3,Ofast,unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;

const char nl = '\n';

void fastIO() {
	ios::sync_with_stdio(false);
	cin.tie(0);
}

int dirX[4] = {-1, 1, 0, 0};
int dirY[4] = {0, 0, -1, 1};

const int MAX = 5005;

int main() {
	fastIO();
	int N;
	cin>>N;
	ll x1, y1, x2, y2;
	cin>>x1>>y1>>x2>>y2;
	x1--; y1--; x2--; y2--;
	vi L(N, 0);
	for (int i = 0; i < N; i++) {
		cin>>L[i];
		L[i]++;
	}
	map<ii, ll> dist;
	queue<ii> q;	// {x, y}
	q.push({x1, y1});
	dist[{x1, y1}] = 0;
	int ctr = 0;
	while (!q.empty()) {
		ctr++;
		if (ctr == 200)
			break;
		ii p = q.front();
//		cout<<"at "<<p.fi<<", "<<p.se<<endl;
		q.pop();
		for (int i = 0; i < 4; i++) {
			ll nx = p.fi + dirX[i];
			ll ny = p.se + dirY[i];
			if (nx < 0 || nx >= N)	// can't go up / down when at edge
				continue;
			if (nx == 0 && ny == -1)	// can't go left at start
				continue;
			if (nx == N - 1 && ny == L[N - 1])	// can't go right at end
				continue;
			// convert to actual become
			if (ny == L[nx] && i == 3) {
				nx++;
				ny = 0;
			}
			else if (ny == -1 && i == 2) {
				nx--;
				ny = L[nx] - 1;
			}
			if (i == 0 || i == 1)
				ny = min(ny, L[nx] - 1);
			if (dist.find({nx, ny}) == dist.end()) {
//				cout<<"going to "<<nx<<" "<<ny<<endl;
				dist[{nx, ny}] = dist[{p.fi, p.se}] + 1;
				q.push({nx, ny});
			}
		}
	}
/*	cout<<"dist: ";
	for (pair<ii, ll> p : dist)
		cout<<"("<<p.fi.fi<<", "<<p.fi.se<<"): "<<p.se<<"; ";
	cout<<endl;*/
	ll ans = 1e18;
	for (pair<ii, ll> p : dist) {
		ans = min(ans, abs(p.fi.fi - x2) + abs(p.fi.se - y2) + p.se);
	}
	cout<<ans<<endl;
}
#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...