이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<v> vv;
typedef vector<vv> vvv;
typedef pair<ll, ll> p;
typedef vector<p> vp;
typedef vector<vp> vvp;
typedef vector<vvp> vvvp;
typedef pair<ll, p> tri;
typedef vector<tri> vtri;
typedef vector<vtri> vvtri;
typedef vector<vvtri> vvvtri;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
const ll INF = 1e18;
const ll mod = 1e9 + 7;
#include "jumps.h"
v H;
ll N;
v nextHigher;
v prevHigher;
v st;
ll n;
ll query(ll l, ll r)
{
l += n;
r += n;
ll res = -INF;
while (l <= r)
{
if (l % 2 == 1)
{
res = max(res, st[l]);
l++;
}
if (r % 2 == 0)
{
res = max(res,st[r]);
r--;
}
l /= 2;
r /= 2;
}
return res;
}
void init(int Nn, std::vector<int> Hh) {
H = v();
for (auto x : Hh) H.pb(x);
N = Nn;
nextHigher = v(N, N);
stack<p> mon;
for (ll i = N - 1; i >= 0; i--)
{
while (!mon.empty() && mon.top().s <= H[i]) mon.pop();
if (!mon.empty()) nextHigher[i] = mon.top().f;
mon.emplace(i, H[i]);
}
mon = stack<p>();
prevHigher = v(N, -1);
for (ll i = 0; i < N; i++)
{
while (!mon.empty() && mon.top().s <= H[i]) mon.pop();
if (!mon.empty()) prevHigher[i] = mon.top().f;
mon.emplace(i, H[i]);
}
n = pow(2, ceil(log2(N)));
st = v(2 * n, -INF);
for (ll i = 0; i < N; i++) st[n + i] = H[i];
for (ll i = n - 1; i> 0; i--) st[i] = max(st[2 * i], st[2 * i + 1]);
}
int minimum_jumps(int A, int B, int C, int D) {
ll mini = INF;
for (ll i = A; i <= B; i++)
{
for (ll j = C; j <= D; j++)
{
if (query(i, j) == H[j])
{
ll steps = 0;
ll cur = i;
while (cur != j)
{
ll next = -1;
if (prevHigher[cur] != -1 && H[prevHigher[cur]] <= H[j]) next = prevHigher[cur];
if (nextHigher[cur] != N && H[nextHigher[cur]] <= H[j] && (next == -1 || H[next] < H[nextHigher[cur]])) next = nextHigher[cur];
steps++;
cur = next;
}
mini = min(mini, steps);
}
}
}
if (mini == INF) mini = -1;
return mini;
}
/*
int main() {
int N, Q;
assert(2 == scanf("%d %d", &N, &Q));
std::vector<int> H(N);
for (int i = 0; i < N; ++i) {
assert(1 == scanf("%d", &H[i]));
}
init(N, H);
for (int i = 0; i < Q; ++i) {
int A, B, C, D;
assert(4 == scanf("%d %d %d %d", &A, &B, &C, &D));
printf("%d\n", minimum_jumps(A, B, C, D));
}
return 0;
}
/**/
컴파일 시 표준 에러 (stderr) 메시지
jumps.cpp:130:1: warning: "/*" within comment [-Wcomment]
130 | /**/
|
# | 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... |