Submission #1134346

#TimeUsernameProblemLanguageResultExecution timeMemory
1134346Alihan_8Text editor (CEOI24_editor)C++20
14 / 100
103 ms39616 KiB
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define size(x) (int)(x).size()
#define int long long

typedef pair <int,int> pii;

using i64 = long long;

template <class F, class _S>
bool chmin(F &u, const _S &v){
	bool flag = false;
	if ( u > v ){
		u = v; flag |= true;
	}
	return flag;
}

template <class F, class _S>
bool chmax(F &u, const _S &v){
	bool flag = false;
	if ( u < v ){
		u = v; flag |= true;
	}
	return flag;
}

const int inf = 1e16;

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
    
    int n, sx, sy, ex, ey;
    cin >> n >> sx >> sy >> ex >> ey;
    
    --sx, --sy, --ex, --ey;
    
    vector <int> l(n);
    vector <vector<int>> dp(n);
    
    for ( int i = 0; i < n; i++ ){
		cin >> l[i]; ++l[i];
		
		dp[i].resize(l[i], inf);
	}
	
	queue <ar<int,2>> q; 
	
	dp[sx][sy] = 0; q.push({sx, sy});
	
	while ( !q.empty() ){
		auto [x, y] = q.front();
		q.pop();
		
		if ( y + 1 == l[x] ){
			if ( x + 1 < n && chmin(dp[x + 1][0], dp[x][y] + 1) ) q.push({x + 1, 0});
		} else if ( chmin(dp[x][y + 1], dp[x][y] + 1) ) q.push({x, y + 1});

		if ( y == 0 ){
			if ( x > 0 && chmin(dp[x - 1][l[x - 1] - 1], dp[x][y] + 1) ) q.push({x - 1, l[x - 1] - 1});
		} else if ( chmin(dp[x][y - 1], dp[x][y] + 1) ) q.push({x,  y - 1});
		
		if ( x > 0 && chmin(dp[x - 1][min(y, l[x - 1] - 1)], dp[x][y] + 1) ) q.push({x - 1, min(y, l[x - 1] - 1)});
		if ( x + 1 < n && chmin(dp[x + 1][min(y, l[x + 1] - 1)], dp[x][y] + 1) ) q.push({x + 1, min(y, l[x + 1] - 1)});
	}
	
	cout << dp[ex][ey];
    	
	cout << '\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...