Logo
(追記) (追記ここまで)

29987번 - Best Fair Shuffles 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
0.5 초 1024 MB42191542.857%

문제

A Fair shuffle is a specific method of shuffling N distinct cards arranged horizontally from left to right. In a Fair shuffle, the cards are divided into two contiguous partitions which may have different card counts, and one of them may even have zero cards. Let’s denote the left partition as L and the right partition as R.

Cards from the left partition (L) are then merged with the cards from the right partition (R) while maintaining the relative order between the cards of each partition.

Given the final permutation obtained after applying K Fair shuffles to an initial sequence of N non-repeating cards numbered from 1 to N, your task is to determine the minimum possible value of K.

For example, if we start with the sequence 1 2 3 4 5 and perform a Fair shuffle by partitioning it into L: 1 2 and R: 3 4 5, cards from L and R can be merged in various ways:

  • 1 3 2 4 5
  • 1 3 4 2 5
  • 3 4 5 1 2
  • 1 2 3 4 5
  • etc

Each arrangement represents a possible outcome of applying a single Fair shuffle. However, 1 3 2 5 4 is not a possible outcome as the relative order of cards 4 and 5 from R is not preserved.

Assume that the outcome of the first Fair shuffle is 1 3 2 4 5. If we perform a second Fair shuffle on it, we could partition the sequence into L: 1 3 2 4 and R: 5 and merge it as 1 3 2 5 4.

입력

The first line contains an integer N(1 ≤ N ≤ 106), the number of cards in the deck. The second line contains a permutation of integer numbers from 1 to N describing the final arrangement of the cards.

출력

Output a single line with an integer K indicating the minimum number of Fair shuffles required to transform the initial sequence into the given one.

제한

예제 입력 1

5
3 4 5 1 2

예제 출력 1

1

예제 입력 2

10
1 6 5 2 10 3 4 8 7 9

예제 출력 2

3

힌트

출처

ICPC > Regionals > Latin America > Sub-Regional Brasil do ACM ICPC > Maratona de Programação da SBC 2023 B번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /