Submission #117417

# Submission time Handle Problem Language Result Execution time Memory
117417 2019-06-15T22:05:42 Z Mamnoon_Siam Railway Trip (JOI17_railway_trip) C++17
0 / 100
2000 ms 6672 KB
#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 5;

int n, k, q;
vector<int> g[maxn];
int lvl[maxn];
int a[maxn];

int dist(int s, int t) {
	memset(lvl, -1, sizeof lvl);
	queue<int> Q;
	lvl[s] = 0;
	Q.emplace(s);
	while(Q.size()) {
		int u = Q.front();
		Q.pop();
		for(int v : g[u]) {
			if(!~lvl[v]) {
				lvl[v] = lvl[u] + 1;
				Q.emplace(v);
			}
		}
	}
	return lvl[t];
}

int main(int argc, char const *argv[])
{
	// freopen("in", "r", stdin);
	cin >> n >> k >> q;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	for(int i = 1; i <= n; i++) {
		for(int j = i + 1; j <= n; j++) {
			if(a[j] >= a[i]) {
				g[i].emplace_back(j);
				break;
			}
		}
		for(int j = i + 1; j <= n; j++) {
			if(a[j] < a[i]) {
				g[i].emplace_back(j);
				break;
			}
		}
		for(int j = i - 1; j >= 1; j--) {
			if(a[j] >= a[i]) {
				g[i].emplace_back(j);
				break;
			}
		}
		for(int j = i - 1; j >= 1; j--) {
			if(a[j] < a[i]) {
				g[i].emplace_back(j);
				break;
			}
		}
	}
	while(q--) {
		int s, t;
		cin >> s >> t;
		cout << dist(s, t) - 1 << endl;
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 3072 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 3072 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2101 ms 6672 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2004 ms 6648 KB Time limit exceeded
2 Halted 0 ms 0 KB -