[HYSBZ 2457] deque

Deque

  HYSBZ - 2457 

Time 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.

 

 1. What kind of number can be composed of a section of it?
S={a 1,a 2,a 3 ...a i-1,a i,a i+1... a n-1,a n}
=> {a 1} => {a 1,a 2,a 3} => {a 5,a 4,a 1,a 2,a 3}
=>{a i,a i-1,a i-3,a i-2,a i+1,a i+2}
It can be seen: If the sorted index formed a "valley", that is, first decreasing, and then incremented.

 

2. How to deal with the same number? The same number of subscript interchangeable, thus affecting the result.
At this time, using greedy strategy, different numbers divided into different sections, the subscript numbers sequentially increase or decrease by the same interval as far as possible on a descending interval or increment. FIG above as an example, S = {3,1,6,2,5,4}, is obtained after dividing the interval S = {{3}, {1,6}, {2,5}, {4}}. The same sequence number interval can be interchanged.
Incrementing a current time interval, if the interval is less than the minimum cut-off point, it becomes a monotonically decreasing.
A current interval is decremented, if the interval is greater than the maximum cut-off point, it becomes monotonically.
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

 

 

Guess you like

Origin www.cnblogs.com/Cindy-Chan/p/11208088.html