이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int n;
vector<int> h;
vector<int> jump[2];
void init(int N, vector<int> H){
n = N, h = H;
fill(jump, jump+2, vector<int>(n, -1));
stack<int> st;
for(int i = n-1; i >= 0; i--){
while(!st.empty() && h[i] > h[st.top()]){
jump[0][st.top()] = i;
st.pop();
}
st.push(i);
}
while(!st.empty()) st.pop();
for(int i = 0; i < n; i++){
while(!st.empty() && h[i] > h[st.top()]){
jump[1][st.top()] = i;
st.pop();
}
st.push(i);
}
while(!st.empty()) st.pop();
}
int minimum_jumps(int A, int B, int C, int D){
vector<int> dis(n, n+1);
vector<bool> bl(n, 0);
queue<int> Q;
for(int i = A; i <= B; i++){
dis[i] = 0;
bl[i] = 1;
Q.push(i);
}
while(!Q.empty()){
auto nd = Q.front();
Q.pop();
if(jump[0][nd] != -1 && !bl[jump[0][nd]]){
bl[jump[0][nd]];
dis[jump[0][nd]] = dis[nd]+1;
Q.push(jump[0][nd]);
}
}
int ans = *min_element(dis.begin()+C, dis.begin()+D+1);
fill(dis.begin(), dis.end(), n+1);
fill(bl.begin(), bl.end(), 0);
for(int i = A; i <= B; i++){
dis[i] = 0;
bl[i] = 1;
Q.push(i);
}
while(!Q.empty()){
auto nd = Q.front();
Q.pop();
if(jump[1][nd] != -1 && !bl[jump[1][nd]]){
bl[jump[1][nd]];
dis[jump[1][nd]] = dis[nd]+1;
Q.push(jump[1][nd]);
}
}
ans = min(ans, *min_element(dis.begin()+C, dis.begin()+D+1));
if(ans > n) ans = -1;
return ans;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |