답안 #345879

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
345879 2021-01-08T11:16:55 Z kshitij_sodani Railway Trip (JOI17_railway_trip) C++14
20 / 100
2000 ms 8644 KB
//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first 
#define b second
#define endl '\n'

int n,k,q;
int it[100001];
int dis[100001];
vector<int> adj[100001];
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n>>k>>q;
	for(int i=0;i<n;i++){
		cin>>it[i];
	}
	/*for(int i=0;i<n;i++){
		cout<<it[i]<<",";
	}
	cout<<endl;*/
	if(true){
		vector<pair<int,int>> ss;
		for(int i=0;i<n;i++){
			while(ss.size()){
				if(ss.back().a<it[i]){
					ss.pop_back();
					continue;
				}
				else{
					break;
				}
			}
			if(ss.size()){
				adj[i].pb(ss.back().b);
				adj[ss.back().b].pb(i);
				/*int mi=-1;
				for(int j=i-1;j>ss.back().b;j--){
					if(it[j]>mi){
						adj[i].pb(j);
						mi=it[j];
					}
				}*/
				//cout<<i<<","<<ss.back().b<<endl;
			}
			ss.pb({it[i],i});
		}
		ss.clear();
		for(int i=n-1;i>=0;i--){
			while(ss.size()){
				if(ss.back().a<it[i]){
					ss.pop_back();
					continue;
				}
				else{
					break;
				}
			}
			if(ss.size()){
				adj[i].pb(ss.back().b);
				adj[ss.back().b].pb(i);
			/*	if(i+1<ss.back().b){
					adj[i].pb(i+1);
				}*/
				/*int mi=-1;
				for(int j=i+1;j<ss.back().b;j++){
					if(it[j]>mi){
						adj[i].pb(j);
						mi=it[j];
					}
				}*/
				//cout<<i<<":"<<ss.back().b<<endl;
			}
			ss.pb({it[i],i});
		}
	}
	
	while(q--){
		int aa,bb;
		cin>>aa>>bb;
		aa--;
		bb--;
		if(it[aa]>it[bb]){
			swap(aa,bb);
		}
		queue<int> ss;
		ss.push(aa);
		for(int i=0;i<n;i++){
			dis[i]=-1;
		}
		dis[aa]=0;

		while(ss.size()){
			int no=ss.front();
			ss.pop();
			for(auto j:adj[no]){
				if(dis[j]==-1){
					dis[j]=dis[no]+1;
					ss.push(j);
				}
			}
		}
		cout<<dis[bb]-1<<endl;
	}





 
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 2 ms 2668 KB Output is correct
8 Correct 2 ms 2668 KB Output is correct
9 Correct 2 ms 2668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2796 KB Output is correct
2 Correct 215 ms 7964 KB Output is correct
3 Correct 249 ms 8044 KB Output is correct
4 Correct 268 ms 8044 KB Output is correct
5 Correct 259 ms 7916 KB Output is correct
6 Correct 252 ms 7916 KB Output is correct
7 Correct 289 ms 8044 KB Output is correct
8 Correct 86 ms 7008 KB Output is correct
9 Correct 92 ms 7396 KB Output is correct
10 Correct 90 ms 7648 KB Output is correct
11 Correct 199 ms 8508 KB Output is correct
12 Correct 206 ms 8508 KB Output is correct
13 Correct 201 ms 8508 KB Output is correct
14 Correct 206 ms 8508 KB Output is correct
15 Correct 204 ms 8508 KB Output is correct
16 Correct 204 ms 8636 KB Output is correct
17 Correct 287 ms 8556 KB Output is correct
18 Correct 283 ms 8644 KB Output is correct
19 Correct 150 ms 8172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2057 ms 8044 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2047 ms 8172 KB Time limit exceeded
2 Halted 0 ms 0 KB -