이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |