Deque
HYSBZ - 2457Time Limit: 1 Sec Memory Limit: 128 MB
Description
Sherry now face a difficult problem, there is need to sort N integers. Sherry hand tool that can be used several deque. She needs in order to deal with this number N, for each number, Sherry can do two things: 1. Create a deque, and the current number as a unique number in the queue; 2. Before the current head number into existing or after the end of the queue. After completion of all processed, Sherry these queues ordered sequence can be obtained after a nondecreasing.
Input
The first row contains an integer N, the number of integers. The following N lines contains an integer Di, where Di represents an integer of processing required.
Output
It contains a single row, for the deque Sherry minimum number required.
Sample Input
6 3 6 0 9 6 3
Sample Output
2
HINT
100% data N ≤ 200000.
analysis:
This question is beginning to think complicated, do not expect a double-stack sorting yesterday in an attempt to push direct mathematical relationship between what the number three can not coexist in a stack. But difficult to achieve, and the time complexity is too high.
This question is likely to be limited to the queue structure, in fact, really is not necessary to construct this series.
Correct answer is actually by "Sherry these queues can be sorted after a sequence of non-drop", think of the sort directly before considering whether to form a number of adjacent intervals.
Sort (A + . 1 , A + . 1 + n-, CMP); A [ 0 ] = .order 0xFFFFFFF ; // determining boundary conditions after preprocessing A [n-+ . 1 ] = .num 0xFFFFFFF ; BOOL Rise = 0 ; int ANS = 0 ; int J = 0 ; // J breakpoint recorded for ( int I = . 1 ; I <= n-; I ++ ) { if (Rise) { BOOL FL = 0 ; // record of whether changes increase or decrease if(A [I] .order <A [J] .order) Rise = 0 , J = I, FL = . 1 ; the while (A [I] == .num A [I + . 1 ] .num) I ++ ; IF (! FL) J = i; } the else { int P = i; // if this interval is monotonically decreasing, increasing or decreasing resistance not changed at this time is the minimum value of i represents the while (a [i] == a .num [I + . 1 ] .num) I ++ ; IF (A [I] .order> A [J] .order) Rise = . 1 , J = I, ANS ++ ; the else J = P; } } IF ! (Rise) ANS ++; / / If the last is monotonically decreasing, not formed valley also considered a separate queues