제출 #1360777

#제출 시각아이디문제언어결과실행 시간메모리
1360777ezzzayText editor (CEOI24_editor)C++20
56 / 100
4101 ms332780 KiB
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
const int N=1e6+5;
int l[N];
vector<int>v[N];
int get(int y, int x){
	auto it=lower_bound(v[y].begin(),v[y].end(),x);
	return it-v[y].begin();
}
signed main(){
	int n;
	cin>>n;
	int y1,x1,y2,x2;
	cin>>y1>>x1>>y2>>x2;
	set<int>st={x1,x2};
	for(int i=1;i<=n;i++){
		cin>>l[i];
		l[i]++;
		st.insert(l[i]);
	}
	for(int i=1;i<=n;i++){
		for(auto a:st){
			if(a<=l[i]){
				v[i].pb(a);
			}
			else break;
		}
	}
	priority_queue<vector<int>>q;
	q.push({0,y1,x1});
	vector<vector<int>> dst(n+10, vector<int>(st.size()+10));
	for(int i=0;i<=n+2;i++){
		for(int j=0;j<=st.size()+2;j++){
			dst[i][j]=1e9;
		}
	}
	dst[y1][get(y1,x1)]=0;
	while(!q.empty()){
		auto vc= q.top();
		int w=vc[0]* (-1), y=vc[1], x=vc[2];
		q.pop();
		if(dst[y][get(y,x)] >w)continue;
		// dragih 
		auto it=lower_bound(v[y].begin(),v[y].end(),x);
		int x2, c , y2;
		if(it!=v[y].begin()){
			y2=y;
			x2= *prev(it);
			c=x-x2;
			if(dst[y2][get(y2,x2)]>w+c){
				dst[y2][get(y2,x2)]=w+c;
				q.push({-(w+c),y2,x2});
			}
		}
		else if(y!=1){
			y2=y-1;
			x2=v[y2].back();
			c=1;
			if(dst[y2][get(y2,x2)]>w+c){
				dst[y2][get(y2,x2)]=w+c;
				q.push({-(w+c),y2,x2});
			}
		}
		
		
		if(it!= prev(v[y].end())){
			y2=y;
			x2=*next(it);
			c=x2-x;
			if(dst[y2][get(y2,x2)]>w+c){
				dst[y2][get(y2,x2)]=w+c;
				q.push({-(w+c),y2,x2});
			}
		}
		else if(y+1<=n){
			y2=y+1;
			x2=v[y2][0];
			c=1;
			if(dst[y2][get(y2,x2)]>w+c){
				dst[y2][get(y2,x2)]=w+c;
				q.push({-(w+c),y2,x2});
			}
		}

		if(y!=1 ){
			int y2=y-1;
			if(l[y2]>=x)x2=x;
			else x2=v[y2].back();
			c=1;
			if(dst[y2][get(y2,x2)]>w+c){
				dst[y2][get(y2,x2)]=w+c;
				q.push({-(w+c),y2,x2});
			}
		}
		if(y!=n){
			int y2=y+1;
			if(l[y2]>=x)x2=x;
			else x2=v[y2].back();
			c=1;
			if(dst[y2][get(y2,x2)]>w+c){
				dst[y2][get(y2,x2)]=w+c;
				q.push({-(w+c),y2,x2});
			}
		}
	}
	cout<<dst[y2][(get(y2,x2))];
	
	
}
#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...