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

25876번 - First Last Sorting 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 1024 MB29201970.370%

문제

Arup has just created a data structure that makes the two following list transformations in constant O(1) time:

  1. Take any element in the list and move it to the front.
  2. Take any element in the list and move it to the back.

You've realized that sorting speed can be improved using these transformations. For example, consider the input list:

  • 8, 3, 6, 7, 4, 1, 5, 2

We can do the following sequence of transformations to sort this list:

  • 8, 3, 7, 4, 1, 5, 2, 6 (move 6 to end)
  • 8, 3, 4, 1, 5, 2, 6, 7 (move 7 to end)
  • 2, 8, 3, 4, 1, 5, 6, 7 (move 2 to front)
  • 1, 2, 8, 3, 4, 5, 6, 7 (move 1 to front)
  • 1, 2, 3, 4, 5, 6, 7, 8 (move 8 to end)

You are now curious. Given an input array of distinct values, what is the fewest number of these first/last operations necessary to sort the array?

Given an initial permutation of the integers 1, 2, ..., n, determine the fewest number of first/last operations necessary to get the list of values sorted in increasing order.

입력

The first line of input will contain a single positive integer, n (n ≤ 105), representing the number of values to be sorted. The next n lines contain one integer each. All of these integers will be distinct values in between 1 and n (inclusive), representing the original order of the data to sort for the input case.

출력

On a line by itself, output the fewest number of first/last operations necessary to sort the input list.

제한

예제 입력 1

8
8
3
6
7
4
1
5
2

예제 출력 1

5

예제 입력 2

5
1
2
5
3
4

예제 출력 2

1

힌트

출처

University > University of Central Florida > 2018 Local Programming Contest 8번

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

출처

대학교 대회

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

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