Submission #1147882

#TimeUsernameProblemLanguageResultExecution timeMemory
1147882choochoo밀림 점프 (APIO21_jumps)C++20
0 / 100
2 ms5184 KiB
#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ve vector
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<ll,ll>

int n, k=18, stmax[18][200001], stmin[18][200001];
ve<int> h, adj[200001];

void init(int N, ve<int> H) {
	n = N; h = H;
	h.pb(999999);
	for (int i=0; i<n; i++) {
		if (h[i] == n-1) {
			adj[i].pb(n);
			continue;
		}
		for (int l=i-1; l>=0; l--) {
			if (h[l] > h[i]) {
				adj[i].pb(l);
				break;
			}
		}
		for (int r=i+1; r<n; r++) {
			if (h[r] > h[i]) {
				adj[i].pb(r);
				break;
			}
		}
	}

	for (int i=0; i<n; i++) {
		if (h[adj[i].front()] > h[adj[i].back()]) {
			stmax[0][i] = adj[i].front();
		} else {
			stmax[0][i] = adj[i].back();
		}
	}
	stmax[0][n] = n;
	for (int i=1; i<=k; i++) {
		stmax[i][n] = n;
		for (int j=0; j<n; j++) {
			stmax[i][j] = stmax[i-1][stmax[i-1][j]];
		}
	}
}

int minimum_jumps(int A, int B, int C, int D) {
	int ans=0, valid=0;
	for (int i=k; i>=0; i--) {
		if (h[stmax[i][A]] == h[C]) {
			ans += 1 << i;
			valid = 1;
			break;
		} else if (h[stmax[i][A]] < h[C]) {
			ans += 1<<i;
			A = stmax[i][A];
		}
	}
	if (!valid) return -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...