답안 #972406

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
972406 2024-04-30T11:44:43 Z akacool445k 밀림 점프 (APIO21_jumps) C++14
0 / 100
3 ms 4696 KB
#include <bits/stdc++.h>
#include "jumps.h"
using namespace std;

// #define int long long
#define ll long long
#define ff first
#define ss second
#define pint pair < int , int >
#define fast ios_base::sync_with_stdio(NULL); cin.tie(NULL)
typedef vector < int > vint;

const int inf = 1e9 + 9;
const int mxn = 2e5 + 2;
const int mod = 1e9 + 7;

int n;
int h[mxn] , le[mxn] , ri[mxn];
int inv[mxn];
int spx[19][mxn] , spn[19][mxn];

void init(int N , vint H) {
	n = N;
	vint v;
	for (int i = 0; i < n; i++) {
		h[i] = H[i];
		inv[h[i]] = i;
	}

	for (int i = 0; i < n; i++) {

		while (v.back() < h[i] && v.size() > 0) {
			ri[v.back()] = h[i];
			v.pop_back();
		}
		if (v.size() > 0) le[h[i]] = v.back();
		v.push_back(h[i]);
	}

	for (int i = 0; i < n; i++) {
		// cout << h[i] << ": " << le[h[i]] << ' ' << ri[h[i]] << '\n';
		spx[0][h[i]] = max(le[h[i]] , ri[h[i]]);
		spn[0][h[i]] = min(le[h[i]] , ri[h[i]]);
		if (spn[0][h[i]] == 0) spn[0][h[i]] = spx[0][h[i]];
	}

	for (int j = 1; j < 19; j++) {
		for (int i = 0; i < n; i++) {
			spx[j][h[i]] = spx[j-1][spx[j-1][h[i]]];
			spn[j][h[i]] = spn[j-1][spn[j-1][h[i]]];
			
			// cout << spx[j][h[i]] << ' ' << spn[j][h[i]] << '\n';
		}
	}
}

int minimum_jumps(int a , int b , int c , int d) {
	if (h[a] > h[d]) return -1;
	int dis = 0;
	for (int i = 18; i >= 0; i--) {
		if (spx[i][h[a]] > h[d]) continue;
		if (spx[i][h[a]] == h[d]) return (1 << i);
		int t = minimum_jumps(inv[spx[i][h[a]]] , b , c , d);
		if(t == -1) {
			return -1;
		}
		return (1 << i) + t;
	}

	for (int i = 18; i >= 0; i--) {
		if (spn[i][h[a]] > h[d]) continue;
		if (spn[i][h[a]] == h[d]) return (1 << i);
		int t = minimum_jumps(inv[spx[i][h[a]]] , b , c , d);
		if(t == -1) {
			return -1;
		}
		return (1 << i) + t;
	}

	return -1;
}

Compilation message

jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:59:6: warning: unused variable 'dis' [-Wunused-variable]
   59 |  int dis = 0;
      |      ^~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 4696 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 344 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 344 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 4696 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 344 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 344 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 4696 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -