답안 #532916

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
532916 2022-03-04T08:29:16 Z vinnipuh01 밀림 점프 (APIO21_jumps) C++17
4 / 100
1158 ms 81716 KB
#include "jump.h"
 
#include <bits/stdc++.h>
#include <vector>
 
using namespace std;
int n, par[ 200001 ][ 21 ], parr[ 200001 ][ 21 ], dist[ 200001 ][ 21 ], distt[ 200001 ][ 21 ];
int lg = 20;
int a[ 200001 ], l[ 200001 ], r[ 200001 ], sum = 0;
vector <int> v[ 200001 ], vv;
vector <pair<int, int> > v1;

bool ok;
 
void init(int N, vector<int> H) {
	n = N;
	for ( int i = 0 ; i < H.size(); i ++ ) {
		a[ i + 1 ] = H[ i ];
		v1.push_back( { a[ i + 1 ], i + 1 } );
	}
	sort( v1.begin(), v1.end() );
	for ( int i = 1; i <= n; i ++ ) {
		while ( vv.size() && a[ vv.back() ] < a[ i ] ) {
			vv.pop_back();
		}
		if ( vv.size() ) {
			v[ i ].push_back( vv.back() );
		}
		vv.push_back( i );
	}
	vv.clear();
	for ( int i = n; i >= 1; i -- ) {
		while ( vv.size() && a[ vv.back() ] < a[ i ] ) {
			vv.pop_back();
		}
		if ( vv.size() )
			v[ i ].push_back( vv.back() );
		vv.push_back( i );
	}
	for ( int i = 1; i <= n; i ++ ) {
		if ( v[ i ].size() == 1 ) {
			parr[ i ][ 0 ] = v[ i ].back();
			par[ i ][ 0 ] = i;
			dist[ i ][ 0 ] = 0;
			distt[ i ][ 0 ] = 1;
		}
		else if ( v[ i ].size() == 2 ) {
			if ( a[ v[ i ].back() ] > a[ v[ i ].front() ] ) {
				par[ i ][ 0 ] = v[ i ].back();
				parr[ i ][ 0 ] = v[ i ].front();
			}
			else {
				par[ i ][ 0 ] = v[ i ].front();
				parr[ i ][ 0 ] = v[ i ].back();
			}
			dist[  i][ 0 ] = distt[ i ][ 0 ] = 1;
		}
		else {
			par[ i ][ 0 ] = parr[ i ][ 0 ] = i;
			dist[ i ][ 0 ] = distt[ i ][ 0 ] = 0;
		}
	}
	for ( int i = v1.size() - 1; i >= 0; i -- ) {
		for ( int j = 1; j <= lg; j ++ ) {
			par[ v1[ i ].second ][ j ] = par[ par[ v1[ i ].second ][ j - 1 ]][ j - 1 ];
			dist[ v1[ i ].second ][ j ] = dist[ par[ v1[ i ].second ][ j - 1 ] ][ j - 1 ] + dist[ v1[ i ].second ][ j - 1 ];
			parr[ v1[ i ].second ][ j ] = parr[ parr[ v1[ i ].second ][ j - 1 ] ][ j - 1 ];
			distt[ v1[ i ].second ][ j ] = distt[ par[ v1[ i ].second ][ j - 1 ] ][ j - 1 ] + distt[ v1[ i ].second ][ j - 1 ];
		}
	}
	for ( int i = n; i > 1; i -- ) {
		if ( a[ i ] <= a[ i - 1 ] )
			ok = 1;
	}
}
 
const long long oo = 1e18;
long long mx, mn;
int c, f;
map <int, long long> d;
map <int, bool> used;
queue <int> q;
 
long long int bfs() {
	mx = oo;
	while ( q.size() ) {
		int g = q.front();
		q.pop();
		for ( auto to : v[ g ] ) {
			if ( !used[ to ] || d[ to ] > d[ g ] + 1 ) {
				d[ to ] = d[ g ] + 1;
				q.push( to );
				if ( to >= c && to <= f ) {
					mx = min( d[ g ] + 1, mx );
				}
				used[ to ] = 1;
			}
		}
	}
	while( q.size() )
		q.pop();
	return mx;
}

pair<int,int> lca( int aa, int b ) {
	int sum = 0;
	for ( int i = lg; i >= 0; i -- ) {
		if ( a[ par[ aa ][ i ] ] <= a[ b ] ) {
			sum += dist[ aa ][ i ];
			aa = par[ aa ][ i ];
		}
	}
	return { aa, sum };
}

int lca1( int aa, int b ) {
	if ( aa == b )
		return 0;
	int sum = 0;
	for ( int i = lg; i >= 0; i -- ) {
		if ( a[ parr[ aa ][ i ] ] < a[ b ] ) {
			sum += distt[ aa ][ i ];
			aa = parr[ aa ][ i ];
		}
	}
	return sum;
}

int minimum_jumps(int A, int B, int C, int D) {
	A ++;
	B ++;
	C ++;
	D ++;
	if ( !ok ) {
		if ( C > B )
			return C - B;
		else
			return A - D;
	}
	if ( A == B && C == D ) {
		pair<int, int> p;
		p = lca( A, C );
		sum = p.second + lca1( p.first, C );
		return sum;
	}
	d.clear();
	used.clear();
	c = C;
	f = D;
	mn = oo;
	for ( int i = A; i <= B; i ++ ) {
		q.push( i );
		used[ i ] = 1;
	}
	mn = bfs(  );
	if ( mn == oo )
		return -1;
  return mn;
}

Compilation message

jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:17:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |  for ( int i = 0 ; i < H.size(); i ++ ) {
      |                    ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4936 KB Output is correct
2 Correct 3 ms 4936 KB Output is correct
3 Correct 219 ms 66012 KB Output is correct
4 Correct 1046 ms 81636 KB Output is correct
5 Correct 985 ms 43648 KB Output is correct
6 Correct 1111 ms 81716 KB Output is correct
7 Correct 846 ms 57860 KB Output is correct
8 Correct 1158 ms 81640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 4936 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 4936 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 4936 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4936 KB Output is correct
2 Incorrect 3 ms 4936 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4936 KB Output is correct
2 Incorrect 3 ms 4936 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4936 KB Output is correct
2 Correct 3 ms 4936 KB Output is correct
3 Correct 219 ms 66012 KB Output is correct
4 Correct 1046 ms 81636 KB Output is correct
5 Correct 985 ms 43648 KB Output is correct
6 Correct 1111 ms 81716 KB Output is correct
7 Correct 846 ms 57860 KB Output is correct
8 Correct 1158 ms 81640 KB Output is correct
9 Incorrect 3 ms 4936 KB Output isn't correct
10 Halted 0 ms 0 KB -