Submission #117417

#TimeUsernameProblemLanguageResultExecution timeMemory
117417Mamnoon_SiamRailway Trip (JOI17_railway_trip)C++17
0 / 100
2101 ms6672 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...