이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "jumps.h"
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <string>
#include <sstream>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <stack>
#include <cstdio>
#include <climits>
#include <tuple>
#include <ctime>
#include <cstring>
#include <numeric>
#include <functional>
#include <chrono>
#include <cassert>
#include <bitset>
#include <fstream>
//#include <bit>
//#include <ranges>
//#include <numbers>
#define sz(a) ((int)((a).size()))
// printf("%.10f\n", ans);
/*ofstream fout("timeline.out");
ifstream fin("timeline.in");*/
using ll = long long;
using namespace std;
const int inf = 1e9;
int n, a, b, c, d, ans;
vector<int> h, dp, nge, pge;
bool task(int id)
{
if (id == 1)
{
for (int i = 0; i < n; i++) if (h[i] != i + 1) return 0;
return 1;
}
/*else if (id == 3)
{
return max(n, q) <= 2000;
}
else if (id == 4)
{
return q <= 5;
}*/
return 0;
}
void NGE()
{
stack<int> s;
s.push(0);
for (int i = 1; i < n; i++)
{
if (s.empty())
{
s.push(i);
continue;
}
while (!s.empty() && h[s.top()] < h[i])
{
nge[s.top()] = i;
s.pop();
}
s.push(i);
}
while (!s.empty())
{
nge[s.top()] = -1;
s.pop();
}
}
void PGE()
{
stack<int> s;
s.push(0);
pge[0] = -1;
for (int i = 1; i < n; i++)
{
while (!s.empty() && h[s.top()] < h[i]) s.pop();
if (s.empty()) pge[i] = -1;
else pge[i] = s.top();
s.push(i);
}
}
int dfs(int u)
{
if (dp[u] != inf + 1) return dp[u];
if (c <= u && u <= d) return dp[u] = 0;
int r = nge[u];
int ans = inf;
if (r != -1) ans = min(ans, dfs(r) + 1);
int l = pge[u];
if (l != -1) ans = min(ans, dfs(l) + 1);
return dp[u] = ans;
}
void init(int N, std::vector<int> H) {
n = N;
h = H;
nge = pge = vector<int>(n);
NGE();
PGE();
}
int minimum_jumps(int A, int B, int C, int D) {
a = A, b = B, c = C, d = D;
if (task(1)) return c - b;
else
{
dp = vector<int>(n, inf + 1);
ans = inf;
for (int i = a; i <= b; i++) ans = min(ans, dfs(i));
if (ans == inf) ans = -1;
return ans;
}
}
# | 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... |