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 "jumps.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5;
int dis[2001][2001];
vector<int> g[MAXN+1];
vector<int> h;
int n, srt;
void init(int N, vector<int> H){
n = N, srt = 1, h = H;
for(int i = 1;i < N; i++){
if(h[i-1] > h[i]) srt = 0;
}
stack<int> st;
st.push(MAXN+MAXN);
for(int i = N-1;i >= 0; i--){
while(h[i] >= st.top()){
st.pop();
}
if(st.top()<= MAXN) g[h[i]].push_back(st.top());
st.push(h[i]);
}
reverse(h.begin(), h.end());
while(!st.empty()) st.pop();
st.push(MAXN+MAXN);
for(int i = N-1;i >= 0; i--){
while(h[i] >= st.top()){
st.pop();
}
if(st.top()<= MAXN) g[h[i]].push_back(st.top());
st.push(h[i]);
}
reverse(h.begin(), h.end());
if(srt) return;
if(n > 2000) return;
memset(dis, -1, sizeof(dis));
queue<int> q;
for(int i = 1;i <= n; i++){
q.push(i);
dis[i][i] = 0;
while(!q.empty()){
int v = q.front(); q.pop();
for(int to : g[v]){
if(dis[i][to] == -1){
dis[i][to] = dis[i][v]+1;
q.push(to);
}
}
}
}
}
int minimum_jumps(int A, int B, int C, int D){
if(srt){
return C-B;
}
int mn = INT_MAX;
if(n <= 2000){
for(int i = A; i <= B; i++){
for(int j = C; j <= D; j++){
if(dis[h[i]][h[j]] != -1) mn = min(mn, dis[h[i]][h[j]]);
}
}
}else{
vector<int> dist(n+1, -1);
queue<int> q;
for(int i = A;i <= B;i++){
q.push(h[i]);
dist[h[i]] = 0;
}
while(!q.empty()){
int v = q.front(); q.pop();
for(int to : g[v]){
if(dist[to] == -1){
dist[to] = dist[v]+1;
q.push(to);
}
}
}
for(int i = C; i <= D; i++) if(dist[h[i]] != -1) mn= min(mn, dist[h[i]]);
}
if(mn == INT_MAX) mn = -1;
return mn;
}
//main(){
// int N; cin >> N;
// vector<int> H(N);
// for(int i = 0;i < N; i++) cin >> H[i];
// init(N, H);
// cout << minimum_jumps(4, 4, 6, 6) << '\n';
// cout << minimum_jumps(1, 3, 5, 6) << '\n';
// cout << minimum_jumps(0, 1, 2, 2) << '\n';
//
//}
# | 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... |