# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
335278 | Plurm | Railway Trip (JOI17_railway_trip) | C++11 | 2066 ms | 12524 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100005]; // cartesian-tree-like graph
int d[100005];
vector<int> bckt[100005];
int l[100005];
int n,k,q;
void compute(int a){
d[a] = 0;
for(int i = 1; i <= k; i++){
for(int x : bckt[i]){
for(int y : g[x]) d[x] = min(d[x], d[y]+1);
}
reverse(bckt[i].begin(), bckt[i].end());
for(int x : bckt[i]){
for(int y : g[x]) d[x] = min(d[x], d[y]+1);
}
reverse(bckt[i].begin(), bckt[i].end());
}
}
int main(){
scanf("%d%d%d",&n,&k,&q);
stack<int> stk;
for(int i = 1; i <= n; i++){
scanf("%d", l+i);
bckt[l[i]].push_back(i);
while(!stk.empty() && l[stk.top()] <= l[i]){
g[i].push_back(stk.top());
stk.pop();
}
if(!stk.empty() && l[stk.top()] == l[i]) g[i].push_back(stk.top());
stk.push(i);
}
while(!stk.empty()) stk.pop();
for(int i = n; i > 0; i--){
while(!stk.empty() && l[stk.top()] <= l[i]){
g[i].push_back(stk.top());
stk.pop();
}
if(!stk.empty() && l[stk.top()] == l[i]) g[i].push_back(stk.top());
stk.push(i);
}
while(q--){
int a,b;
scanf("%d%d",&a,&b);
for(int i = 1; i <= n; i++) d[i] = 1e9;
compute(a);
vector<int> v(n);
for(int i = 1; i <= n; i++) v[i] = d[i];
for(int i = 1; i <= n; i++) d[i] = 1e9;
compute(b);
int ans = 1e9;
for(int i = 1; i <= n; i++){
ans = min(ans, v[i] + d[i]);
}
printf("%d\n", ans-1);
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |