Complexity Measure

시간 제한3초메모리 제한1024 MB

요약
순서열 X[i..n]에서 노드의 이진 검색 트리 부모가 시작 위치 i가 변할 때 바뀌는 횟수의 합을 계산합니다.
난이도

어려움10점 중 9점

유형
동적 계획법, 트리, 비트 연산, 이분 탐색
정답자
아직 제출이 없습니다

문제

As the number of International Criminals of Poor Coding (ICPC) has been increasing rapidly, the government established a specialized prison. Now these ICPCs, the programmers who have written extremely bad codes, are supposed to be arrested and brutally punished in this place. One of the harsh punishments is drawing binary search trees. Every morning, the prisoners are given an integer sequence, and they are forced to drawseveral binary search trees according to the certain rules.

More precisely, given a sequence X=x_1,x_2,⋯ ,x_nX = x\_1, x\_2, \cdots , x\_n of nn distinct integers, let T(X)T(X) be the binary search tree constructed by performing the standard way of insertion of each element of XX in order; note that it holds for every 1≤i<j≤n1 ≤ i < j ≤ n that the node with key x_ix\_i cannot be a descendant of the node with key x_jx\_j. Then the prisoners must draw n−1n - 1 binary search trees T(X\[1..n]),T(X\[2..n]),⋯ ,T(X\[n−1..n])T(X\[1. . n]), T(X\[2. . n]), \cdots , T(X\[n - 1. . n]) where X\[i..n]X\[i. . n] is the suffix sequence x_i,x_i+1,⋯ ,x_nx\_i , x\_{i+1}, \cdots , x\_n of XX. For ease of verification, the prisoners must fill a table representing the trees. This table has n−1n - 1 rows and nn columns and the cell in the ii-th row and jj-th column has to be filled with the key of the parent of the node with key x_jx\_j in the binary search tree T(X\[i..n])T(X\[i. . n]).

For example, if X=2,4,5,3,1X = 2,4,5,3,1, the cell in the second row and the fifth column needs to be filled with 33, because the parent of the node with key x_5(=1)x\_5(= 1) in T(X\[2..5])T(X\[2. .5]) has the key 33. The binary search trees and the entire table for XX are as in Fig. 1.

(a) T(X\[1..5])T(X\[1. .5])(b) T(X\[2..5])T(X\[2. .5])(e) Table
(c) T(X\[3..5])T(X\[3. .5])(d) T(X\[4..5])T(X\[4. .5])

Fig. 1 Binary search trees for X=2,4,5,3,1X = 2, 4, 5, 3, 1 and the table representation.

One day, the prison administrators noticed that some sequences are too easy to fill the table while others are not. For example, consider a sequence X′=8,7,1,2,3,6,5,4X' = 8,7,1,2,3,6,5,4. When looking at its table, one can notice that each row is a suffix of the first row (Fig. 2(a)). Such an easy case should not be given to these guilty prisoners. On the other hand, another sequence X′=6,4,2,7,1,8,3,5X' = 6,4,2,7,1,8,3,5 is more complicated in the sense that the table has many pairs of adjacent nonempty cells in the same column with different values (indicated with red boxes in Fig. 2(b)). Such pairs are called critical changes. In short, X′X' has no critical changes while X' ' has 99 critical changes, from which one can say that X' ' is more difficult than X′X'. The prison administrators want to use the number of critical changes as a complexity measure of a sequence so that they can control the difficulty of the tree-drawing task.

(a) Table for X′X'(b) Table for X' '

Fig. 2 Tables for two sequences X′=8,7,1,2,3,6,5,4X' = 8, 7, 1, 2, 3, 6, 5, 4 and X′=6,4,2,7,1,8,3,5X' = 6, 4, 2, 7, 1, 8, 3, 5. The numbers of critical changes for X′X' are X' ' are 00 and 99, respectively (indicated with red boxes).

Given a sequence of nn distinct integers, write a program to output the number of critical changes of the sequence.

입력

Your program is to read from standard input. The input consists of two lines. The first line contains an integer nn (2≤n≤250,0002 ≤ n ≤ 250\\,000), where nn is the length of the sequence. The following line contains nn integers that comprise the input sequence. The integers are distinct and range from 11 to nn.

출력

Your program is to write to standard output. Print exactly one integer indicating the number of critical changes of the sequence.

예제3

  1. 예제 1

    입력
    5
    2 4 5 3 1
    
    예상 출력
    2
    
  2. 예제 2

    입력
    8
    8 7 1 2 3 6 5 4
    
    예상 출력
    0
    
  3. 예제 3

    입력
    8
    6 4 2 7 1 8 3 5
    
    예상 출력
    9