Poj2452 Sticks Problem -- double-direction monotonous Stack

Source: Internet
Author: User
Sticks Problem
Time Limit:6000 MS Memory Limit:65536 K
Total Submissions:7894 Accepted:1960

Description

Xuanxuan has n sticks of different length. one day, she puts all her sticks in a line, represented by S1, S2, S3 ,... sn. after measuring the length of each stick Sk (1 <= k <= n), she finds that for some sticks Si and Sj (1 <= I <j <= n ), each stick placed between Si and Sj is longer than Si but shorter than Sj.

Now given the length of S1, S2, S3 ,... Sn, you are required to find the maximum value j-I.

Input

The input contains multiple test cases. Each case contains two lines.
Line 1: a single integer n (n <= 50000), indicating the number of sticks.
Line 2: n different positive integers (not larger than 100000), indicating the length of each stick in order.

Output

Output the maximum value j-I in a single line. If there is no such I and j, just output-1.

Sample Input

45 4 3 646 5 4 3

Sample Output

1-1
Analysis: the question is,
There are many groups of wooden sticks. Each group reads one n, which is the number of wooden sticks in this group,
The defined perfect interval indicates that all wooden sticks in the interval are longer than the left endpoint and shorter than the right endpoint,
Find the longest perfect range.
First, we can see the n ^ 2 algorithm of enumeration,
Due to the large data range,
I want to combine the monotonous queue and rmq, because I won't use the rmq st algorithm,
So I chose another method,
Monotonous stack to solve the problem.
 
For specific points, monotonous stack operations are performed in both directions,
Positive. Locate the first number not greater than this number, record location,
Reverse: locate the first number not less than this number, record location,
Scan again,
For each vertex, we check the vertex between the vertex and the right boundary of the vertex,
Check whether the left boundary of a vertex is smaller than or equal to the position of the original vertex,
If yes, the interval between the two points is the interval that satisfies the meaning of the question,
Determine whether it is the longest perfect interval and update the value.
View Code 

1 program poj_2452;
2 var
3 I, j, n, m, k, t, B: longint;
4 s, w, a, l, r: array [0 .. 50001] of longint;
5 begin
6 assign (input, 'poj. in ');
7 reset (input );
8 while not eof do // process data in each group
9 begin
10 fillchar (l, sizeof (l), 0 );
11 fillchar (r, sizeof (r), 0 );
12 fillchar (w, sizeof (w), 0 );
13 fillchar (a, sizeof (a), 0 );
14 fillchar (s, sizeof (s), 0 );
15 // These fillchar were originally added for insurance purposes, which is of little use to this question
16 readln (n );
17 a [0]: = maxlongint;
18 a [n + 1]: =-maxlongint; // manually create a boundary
19 if n = 0 then break; // prevents empty rows of data.
20 m: = 0;
21 for I: = 1 to n do read (a [I]); // read
22 readln;
23 // s is the stack, which ensures its monotonicity.
24 // l and r respectively record the left and right boundary extended by the wooden stick
25 {
26. The representation method here is as follows,
27. For any wooden stick x
28 l [x] <x and l [x-1]> = x
29 r [x]> x and r [x + 1] <= x
30
31 l [x] ~ X-1 sticks are shorter than x
32 x + 1 ~ R [x] sticks are longer than x
33}
34 t: = 1; s [1]: = a [0]; w [1]: = 0;
35 // positive monotonous stack operation, initialization, set the maximum value in the stack
36 // (because the left side of some wooden sticks is shorter than it, there will be no value in l [x)
37 // w stores the positions of the corresponding wooden sticks in the stack in the original sequence.
38 for I: = 1 to n + 1 do
39 begin
40 k: = a [I];
41 while (t> 0) and (k <= s [t]) do
42 begin
43 r [w [t]: = I-1; // assign a value to the right boundary of the element at the top of the stack
44 dec (t); // stack play Operation
45 end;
46 inc (t );
47 s [t]: = k; w [t]: = I; // Add new elements to the stack
48 end;
49 // below is the reverse monotonic Stack
50 t: = 1; s [1]: = a [n + 1]; w [1]: = n + 1;
51 for I: = n downto 0 do
52 begin
53 k: = a [I];
54 while (t> 0) and (k> = s [t]) do
55 begin
56 l [w [t]: = I + 1;
57 dec (t );
58 end;
59 inc (t );
60 s [t]: = k; w [t]: = I;
61 end;
62 {
Below 63 scan operations
64. Search for the intervals that meet the requirements,
65 and Update maximum m
66}
67 for I: = 1 to n do
68 for j: = m + I + 1 to r [I] do // pay attention to this recurrence,
69 // I used to fight like this
70 // for j: = I + 1 to r [I] do
71 // but severe timeout (6000 MS +)
72 // use the current method to reduce the amount of circulation, You Can AC
73 // time consumed: 700 MS +
74 if l [j] <= I then
75 if j-I> m then m: = j-I; // update operation
76 if m = 0 then writeln (-1)
77 else writeln (m); // output
78 end;
79 close (input );
80 end.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.