제출 #553764

#제출 시각아이디문제언어결과실행 시간메모리
553764Zhora_004밀림 점프 (APIO21_jumps)C++17
37 / 100
4082 ms10256 KiB
#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;
bool task1;
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();
    if (task(1)) task1 = 1;
}


int minimum_jumps(int A, int B, int C, int D) {
	a = A, b = B, c = C, d = D;
	if (task1) 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...