이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;
int n, flag;
int X[200005], Y[200005];
int par[20][200005], par2[20][200005];
void init(int N, vector<int> H){
n = N;
stack<int>s;
for(int i=0;i<n;i++){
while(!s.empty() && H[s.top()] < H[i])s.pop();
X[i] = (s.empty() ? -1 : s.top());
par[0][i] = X[i];
s.push(i);
}
flag = 1;
for(int i=0;i<n;i++)if(H[i] != i + 1)flag= 0;
while(!s.empty())s.pop();
for(int i=n-1;i>=0;i--){
while(!s.empty() && H[s.top()] < H[i])s.pop();
Y[i] = (s.empty() ? -1 : s.top());
par2[0][i] = Y[i];
s.push(i);
}
for(int i=1;i<=19;i++)for(int j=0;j<n;j++)par[i][j] = par[i-1][par[i-1][j]];
for(int i=1;i<=19;i++)for(int j=0;j<n;j++)par2[i][j] = par2[i-1][par2[i-1][j]];
}
int minimum_jumps(int a, int b, int c, int d){
if(flag){
if(a > d)return -1;
else if(b >= c || a >= d)return 0;
else return c-b;
}
if(a == b && c == d){
if(a < c){
int ans = 0, tmp = a;
for(int i=19;i>=0;i--)if(par2[i][tmp] <= c && par2[i][tmp] > a)tmp = par2[i][tmp], ans += (1 << i);
if(tmp == c)return ans;
else return -1;
}
else{
int ans = 0, tmp = a;
for(int i=19;i>=0;i--)if(par[i][tmp] >= c)tmp = par[i][tmp], ans += (1 << i);
return (tmp == c ? ans : -1);
}
}
queue<int>q;
int dist[n+1];
for(int i=0;i<n;i++)dist[i] = 1e9;
for(int i=a;i<=b;i++)dist[i] = 0, q.push(i);
while(!q.empty()){
int x = q.front();q.pop();
if(X[x] != -1 && dist[X[x]] > dist[x] + 1)dist[X[x]] = dist[x] + 1, q.push(X[x]);
if(Y[x] != -1 && dist[Y[x]] > dist[x] + 1)dist[Y[x]] = dist[x] + 1, q.push(Y[x]);
}
int mini = 1e9;
for(int i=c;i<=d;i++)mini = min(mini, dist[i]);
return (mini == 1e9 ? -1 : mini);
}
# | 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... |