[Luogu1638] visiting the exhibition (monotone queue)

 

Quite apparent monotony queue up ......

 

Title Description

AsiaWorld-Expo is being exhibited by the best in the world M painters painted pictures.

wangjy think Expo to see that several masters.

However, where the Expo have a very strange rule is that two figures must be stated in the purchase of tickets,

a and b, between the representatives of all the pictures he wanted to see the exhibition of a web through b picture (and contains a b), while tickets

The price is a picture of one yuan.

To see more teacher's paintings, wangjy would like to see a picture after the admission of all teachers (at least each one).

But he wanted to save money. . .

As wangjy friend, he asks you to write a program that he decided a and b values ​​when buying tickets.

Input Format

The first row is N and M, representing the total number of pictures and Expo hall The pictures are drawn by how many bits the teacher

The painting.

Subsequent row comprising N digital, they are between 1 and M, representing the number of bits teacher.

Output Format

a and b (a <= b) is separated by a space character.

To ensure a solution, if multiple solutions, minimum output a.

Sample input and output

Input # 1
12 5
2 5 3 1 3 2 4 1 1 5 4 3
Output # 1
2 7

Description / Tips

30% of the agreed data N <= 200, M <= 20

60% of the data N <= 10000, M <= 1000

100% data N <= 1000000, M <= 2000

#include <stdio.h>
 int m [ 2001 ], NUM;
 int n-[ 1000000 ];
 int main () 
{ 
    int R & lt = - . 1 , L = 0 , N, M, T, I = 0 , ansL, ansR; // R must be -1, since the latter are enumerated beginning from 0 
    Scanf ( " % D% D " , & N, & M);
     for (I = 0 ; I <N; I ++) Scanf ( " % D " , + n- I); 
    I = 0 ;
     / * the first cycle, is shifted right and the right end of the interval, and maintains the number of occurrences artist painting, the emergence of new artist 
    to a counter is incremented, until you see all the artist Videos , then loop determines whether a left end point artist occur more than once, 
this time the application greedy idea, as long as there have been more than once, then you will be able to pop up the left point, because this point 
presence will only make a longer interval, the interval will not contain more artist, as long as we ensure that each zone has painter painting on the list 
at this time by about two variables to record the current interval as the initial endpoint answers * / 
    the while (NUM! = m) 
    {   // cycle through all the painter painting 
        IF (m [n-[I]] == 0 ) NUM ++;   // If the artist has not yet appeared 
        m [n-[I]] ++;   // maintain the painter painting occurrences ([i] n the artist painting) 
        R & lt ++;   // interval continues right end of the right shift 
        I ++ ; 
    } 
    the while (m [n-[L]]> . 1 )   // if more than once when the leftmost painter painting as 
        m [n-[L ++]] -;   // the number of occurrences reduced by one artist, left endpoint moves to the left (left pop-up point, the right indent 
    ansL = L; ansR = R & lt;
    
    / *  then of second cycle, the cycle continues the rest of the painting,
Each time the right point +1, maintenance painter painting occurrences, and no need to continue to pop up the left point exists, 
as in the second cycle in any interval of time are guaranteed and can see all the artist's paintings, 
so inside the loop should determine whether the current range than the known range of answers shorter, if shorter, the answer to update 
my code range from zero dollars, so the output +1 
while loop nest, but only because each point up time out interval will be, so the complexity of O (n), just slightly larger constant * / 
    the while (I <N)   // number Videos 
    { 
        m [n-[I]] ++ ; 
        R & lt ++ ; 
        I ++ ;
         the while ( m [n-[L]]> . 1 ) 
            m [n-[L ++]] - ;
         IF (ansR-ansL> the R- L) 
        { 
            ansR = R & lt; 
            ansL = L; 
        }
    }
    printf("%d %d",ansL+1,ansR+1);
}

 

Guess you like

Origin www.cnblogs.com/phemiku/p/11403592.html