이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
template<class X, class Y>
inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAXN = 200005;
int h[MAXN], dist[MAXN], nxt[MAXN], prv[MAXN], nArr;
bool check_sub1;
void init(int _N, vector<int> _H) {
nArr = _N;
check_sub1 = 1;
for (int i = 1; i <= nArr; ++i) {
h[i] = _H[i - 1];
check_sub1 &= (i == 1 || h[i] > h[i - 1]);
}
stack<int> st;
for (int i = 1; i <= nArr; ++i) {
while(sz(st) && h[st.top()] <= h[i])
st.pop();
prv[i] = (sz(st)) ? st.top() : 0;
st.push(i);
}
while(sz(st))
st.pop();
for (int i = nArr; i > 0; --i) {
while(sz(st) && h[st.top()] <= h[i])
st.pop();
nxt[i] = (sz(st)) ? st.top() : nArr + 1;
st.push(i);
}
}
int brute(int a, int b, int c, int d) {
for (int i = 1; i <= nArr; ++i)
dist[i] = -1;
queue<int> qu;
for (int i = a; i <= b; ++i) {
qu.push(i);
dist[i] = 0;
}
while(sz(qu)) {
int u(qu.front()); qu.pop();
if(u >= c && u <= d)
return dist[u];
if(prv[u] > 0 && dist[prv[u]] < 0) {
dist[prv[u]] = dist[u] + 1;
qu.push(prv[u]);
}
if(nxt[u] <= nArr && dist[nxt[u]] < 0) {
dist[nxt[u]] = dist[u] + 1;
qu.push(nxt[u]);
}
}
return -1;
}
int minimum_jumps(int a, int b, int c, int d) {
++a, ++b, ++c, ++d;
if(check_sub1)
return c - b;
return brute(a, b, c, d);
}
#ifdef Nhoksocqt1
int main(void) {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define TASK "jumps"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
vector<int> h;
int n, q;
cin >> n >> q;
h.resize(n);
for (int i = 0; i < n; ++i)
cin >> h[i];
init(n, h);
for (int t = 0; t < q; ++t) {
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << "ANSWER " << a << ' ' << b << ' ' << c << ' ' << d << ": " << minimum_jumps(a, b, c, d) << '\n';
}
return 0;
}
#endif // Nhoksocqt1
# | 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... |