# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
335279 | Plurm | Railway Trip (JOI17_railway_trip) | C++11 | 2086 ms | 12524 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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+1);
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;
}
컴파일 시 표준 에러 (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... |