이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for (int i = (int)a; i <= (int)b; ++i)
#define rep1(i, a, b) for (int i = (int)a; i >= (int)b; --i)
#define mp make_pair
const int N = 2e5 + 5;
int L[N], R[N], h[N];
int my_size;
void cal_LR() {
stack<int> st;
rep(i, 1, my_size) {
while (!st.empty() && h[st.top()] < h[i]) st.pop();
if (!st.empty()) L[i] = st.top();
st.push(i);
}
while (!st.empty()) st.pop();
rep1(i, my_size, 1) {
while (!st.empty() && h[st.top()] < h[i]) st.pop();
if (!st.empty()) R[i] = st.top();
st.push(i);
}
}
int minimum_jumps(int a, int b, int c, int d) {
++a, ++b, ++c, ++d;
queue<pii> q;
vector<bool> vst(my_size + 1);
vst[0] = 1;
rep(i, a, b) q.push({i, 0}), vst[i] = 1;
while (!q.empty()) {
auto [u, w] = q.front();
q.pop();
if (c <= u && u <= d) return w;
int v = L[u];
if (!vst[v]) {
q.push({v, w + 1});
vst[v] = 1;
}
v = R[u];
if (!vst[v]) {
q.push({v, w + 1});
vst[v] = 1;
}
}
return -1;
}
void init(int sz, vector<int> array) {
my_size = sz;
rep(i, 1, my_size) h[i] = array[i - 1];
cal_LR();
}
// int main() {
// // ios_base::sync_with_stdio(0);
// // cin.tie(0); cout.tie(0);
// int x; cin >> x;
// vector<int> array(x);
// rep(i, 1, x) cin >> array[i - 1];
// init(x,array);
// rep(i, 1, my_size) cout << L[i] << ' ' << R[i]<< '\n';
// int q; cin >> q;
// while (q--) {
// int A, B, C, D;
// cin >> A >> B >> C >> D;
// cout << minimum_jumps(A,B,C,D) << '\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... |