이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
//#include "jumps.h"
#define FIXED_FLOAT(x) std::fixed <<std::setprecision(20) << (x)
#define all(v) (v).begin(), (v).end()
using namespace std;
#define forn(i,n) for (int i = 0; i < (n); ++i)
#define rforn(i, n) for(int i = (n) - 1;i >= 0;--i)
#define sz(x) (int)x.size()
#define ff first
#define se second
#define mp make_pair
using ll = long long;
int mod = (ll)1e9 + 7;
const int INF = 1e9 + 1;
const double eps = 1e-7;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template<class T, size_t SZ> using AR = array<T, SZ>;
template<class T> using PR = pair<T, T>;
template <typename XPAX>
bool ckma(XPAX &x, XPAX y) {
return (x < y ? x = y, 1 : 0);
}
template <typename XPAX>
bool ckmi(XPAX &x, XPAX y) {
return (x > y ? x = y, 1 : 0);
}
const int mxn = 2e5+2;
const int G = 20;
int F[mxn][G], L[mxn][G], R[mxn][G];
V<int> H;
int n;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
void init(int N, V<int> a) {
H = a;
H.insert(H.begin(), INF);
H.push_back(INF);
//debug(H);
n = N;
stack<int> st;
forn(i, N+2) {
while(sz(st) && H[st.top()] <= H[i])
st.pop();
L[i][0] = sz(st) ? st.top() : i;
st.push(i);
}
while(sz(st))st.pop();
rforn(i, N+2) {
while(sz(st) && H[st.top()] <= H[i])
st.pop();
R[i][0] = sz(st) ? st.top() : i;
st.push(i);
}
forn(i,N+2) F[i][0] = (H[L[i][0]] < H[R[i][0]] ? R[i][0] : L[i][0]);
for(int j = 1;j < G;++j) forn(i,N+2) {
L[i][j] = L[L[i][j-1]][j-1];
R[i][j] = R[R[i][j-1]][j-1];
F[i][j] = F[F[i][j-1]][j-1];
}
}
int minimum_jumps(int A, int B, int C, int D) {
++A, ++B, ++C, ++D;
if(B + 1 == C) {
return R[B][0] <= D ? 1 : -1;
}
int md = B + 1;
// md is biggest element in (B,C)
// we have to cross through md eventually
rforn(i, G) if(R[md][i] < C)md = R[md][i];
if(R[md][0] > D)return -1;
if(H[B] > H[md]) {
return R[B][0] <= D ? 1 : -1;
}
int go = B;
// the optimal is where the H[index] in greatest integer < H[mid] in range [A, B]
// go is where we start from
rforn(i, G) if(L[go][i] >= A && H[L[go][i]] < H[md]) go = L[go][i];
int ret = 0;
if(L[go][0] >= A) {
if(R[L[go][0]][0] <= D)return 1;
}
else {
rforn(i, G) if(H[F[go][i]] <= H[md]) {
go = F[go][i];
ret += 1 << i;
}
if(go == md)return R[go][0] <= D ? ret+1 : -1;
else if(L[go][0] && R[L[go][0]][0] <= D)return ret+2;
}
rforn(i, G) if(R[go][i] < C) {
ret += 1 << i;
go = R[go][i];
}
go = R[go][0];
if(C <= go && go <= D)return ret+1;
else return -1;
}
/*
int main() {
init(7, {3, 2, 1, 6, 4, 5, 7});
cout << minimum_jumps(3, 4, 6, 6) << '\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... |