//#include "jumps.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
int n, a, b, c, d;
vector<int> h, pge, nge, dp;
vector<vector<int>> gp;
bool arajitest = 1;
vector<vector<int>> up1, up2;
vector<int> pge_(vector<int> v)
{
vector<int> tmp(v.size());
stack<int> s;
s.push(0);
tmp[0] = -1;
for (int i = 1; i < v.size(); i++)
{
while (!s.empty() && v[s.top()] < v[i]) s.pop();
tmp[i] = (s.empty() ? -1 : s.top());
s.push(i);
}
return tmp;
}
vector<int> nge_(vector<int> v)
{
stack<int> s;
vector<int> tmp(v.size(), -1);
for (int i = 0; i < n; i++)
{
while (!s.empty() && v[s.top()] < v[i]) tmp[s.top()] = i, s.pop();
s.push(i);
}
return tmp;
}
void dfs(int u)
{
if (u >= c && u <= d) { dp[u] = 0; return; }
if (dp[u] != 2e9) return;
if (gp[u].size() > 0) dfs(gp[u][0]);
if (gp[u].size() > 1) dfs(gp[u][1]);
int x = (gp[u].size() > 0 ? dp[gp[u][0]] : 2e9);
int y = (gp[u].size() > 1 ? dp[gp[u][1]] : 2e9);
dp[u] = min(x, y) + 1;
}
void make1(int u, int p)
{
up1[u][0] = p;
for (int i = 1; i < 20; i++) up1[u][i - 1] != 2e9 ? up1[u][i] = up1[up1[u][i - 1]][i - 1] : 1;
int x = (gp[u].size() > 0 ? gp[u][0] : 2e9);
int y = (gp[u].size() > 1 ? gp[u][1] : 2e9);
if (min(x, y) != 2e9) make1(min(x, y), u);
}
void make2(int u, int p)
{
up2[u][0] = p;
for (int i = 1; i < 20; i++) up2[u][i - 1] != 2e9 ? up2[u][i] = up2[up2[u][i - 1]][i - 1] : 1;
int x = (gp[u].size() > 0 ? gp[u][0] : 2e9);
int y = (gp[u].size() > 1 ? gp[u][1] : 2e9);
if (max(x, y) != 2e9) make2(max(x, y), u);
else if (min(x, y) != 2e9) make2(min(x, y), u);
}
void init(int N, vector<int> H)
{
n = N;
h = H;
pge = pge_(H);
nge = nge_(H);
gp = vector<vector<int>>(n);
for (int i = 0; i < n; i++)
{
if (pge[i] != -1) gp[i].push_back(pge[i]);
if (nge[i] != -1) gp[i].push_back(nge[i]);
}
for (int i = 0; i < n; i++) if (H[i] != i + 1) arajitest = 0;
}
int minimum_jumps(int A, int B, int C, int D)
{
a = A, b = B, c = C, d = D;
if (arajitest) return C - B;
else if (a != b && c != d) {
dp = vector<int>(n, 2e9);
int mn = 2e9;
for (int i = a; i <= b; i++) dfs(i), mn = min(mn, dp[i]);
return (mn >= 2e9 ? -1 : mn);
}
else {
up1 = up2 = vector<vector<int>>(n, vector<int>(20, 2e9));
make1(A, 2e9);
make2(A, 2e9);
int G = A, len = 0;
for (int i = 19; i >= 0; i--)
{
if (h[up2[G][i]] <= h[C]) G = up2[G][i], len += (1 << i);
}
if (G == C) return len;
for (int i = 19; i >= 0; i--)
{
if (h[up1[G][i]] <= h[C]) G = up1[G][i], len += (1 << i);
}
return G == C ? len : -1;
}
}
Compilation message
jumps.cpp: In function 'std::vector<int> pge_(std::vector<int>)':
jumps.cpp:20:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for (int i = 1; i < v.size(); i++)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
114 ms |
12072 KB |
Output is correct |
4 |
Correct |
743 ms |
15136 KB |
Output is correct |
5 |
Correct |
700 ms |
7724 KB |
Output is correct |
6 |
Correct |
833 ms |
15148 KB |
Output is correct |
7 |
Correct |
556 ms |
10416 KB |
Output is correct |
8 |
Correct |
764 ms |
15160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
336 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
336 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
336 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Runtime error |
1 ms |
336 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Runtime error |
1 ms |
336 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
114 ms |
12072 KB |
Output is correct |
4 |
Correct |
743 ms |
15136 KB |
Output is correct |
5 |
Correct |
700 ms |
7724 KB |
Output is correct |
6 |
Correct |
833 ms |
15148 KB |
Output is correct |
7 |
Correct |
556 ms |
10416 KB |
Output is correct |
8 |
Correct |
764 ms |
15160 KB |
Output is correct |
9 |
Runtime error |
1 ms |
336 KB |
Execution killed with signal 11 |
10 |
Halted |
0 ms |
0 KB |
- |