Complexity Measure
시간 제한3초메모리 제한1024 MB
순서열 X[i..n]에서 노드의 이진 검색 트리 부모가 시작 위치 i가 변할 때 바뀌는 횟수의 합을 계산합니다.
문제
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 of distinct integers, let be the binary search tree constructed by performing the standard way of insertion of each element of in order; note that it holds for every that the node with key cannot be a descendant of the node with key . Then the prisoners must draw binary search trees where is the suffix sequence of . For ease of verification, the prisoners must fill a table representing the trees. This table has rows and columns and the cell in the -th row and -th column has to be filled with the key of the parent of the node with key in the binary search tree .
For example, if , the cell in the second row and the fifth column needs to be filled with , because the parent of the node with key in has the key . The binary search trees and the entire table for are as in Fig. 1.
Fig. 1 Binary search trees for 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 . 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 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, has no critical changes while X' ' has critical changes, from which one can say that X' ' is more difficult than . 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.
Fig. 2 Tables for two sequences and . The numbers of critical changes for are X' ' are and , respectively (indicated with red boxes).
Given a sequence of 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 (), where is the length of the sequence. The following line contains integers that comprise the input sequence. The integers are distinct and range from to .
출력
Your program is to write to standard output. Print exactly one integer indicating the number of critical changes of the sequence.
(a)
(b)
(e) Table
(c)
(d)
(a) Table for
(b) Table for