POJ 2452 RMQ + binary

Source: Internet
Author: User

Solution:

Enumerate each position I and find the position j on the Right of I that is smaller than a [I ].

Locate the maximum value between I to j-1 k. If a [k]> a [I], update the answer.

 

 

[Cpp]
# Include <iostream>
# Include <algorithm>
# Include <cstring>
# Include <string>
# Include <cstdio>
# Include <cmath>
# Include <queue>
# Include <map>
# Include <set>
# Define eps 1e-5
# Define maxn55555
# Define MAXM 111111
# Define INF 1000000000
Using namespace std;
Int mi [MAXN] [17], mx [MAXN] [17], w [MAXN];
Int Log [MAXN];
Int n;
Void rmqinit (int n)
{
For (int I = 1; I <= n; I ++) mi [I] [0] = mx [I] [0] = I;
Int m = (int) (log (n * 1.0)/log (2.0 ));
For (int I = 1; I <= m; I ++)
For (int j = 1; j <= n; j ++)
{
Mx [j] [I] = mx [j] [I-1];
Mi [j] [I] = mi [j] [I-1];
If (j + (1 <(I-1) <= n)
{
If (w [mx [j] [I] <w [mx [j + (1 <(I-1)] [I-1]) mx [j] [I] = mx [j + (1 <(I-1)] [I-1];
If (w [mi [j] [I]> w [mi [j + (1 <(I-1)] [I-1]) mi [j] [I] = mi [j + (1 <(I-1)] [I-1];
}
}
}
Int rmqmin (int l, int r)
{
Int m = Log [r-l + 1];
If (w [mi [l] [m]> w [mi [r-(1 <m) + 1] [m]) return mi [r-(1 <m) + 1] [m];
Else return mi [l] [m];
}
Int rmqmax (int l, int r)
{
Int m = Log [r-l + 1];
If (w [mx [l] [m] <w [mx [r-(1 <m) + 1] [m]) return mx [r-(1 <m) + 1] [m];
Else return mx [l] [m];
}
Int bin (int x, int l, int r)
{
Int ret =-1;
While (l <= r)
{
Int m = (l + r)> 1;
If (w [x] <w [rmqmin (l, m)])
L = m + 1, ret = max (ret, m );
Else r = m-1;
}
Return ret;
}
 
Int main ()
{
Log [1] = 0;
For (int I = 2; I <MAXN; I ++) Log [I] = Log [I> 1] + 1;
While (scanf ("% d", & n )! = EOF)
{
For (int I = 1; I <= n; I ++) scanf ("% d", & w [I]);
Rmqinit (n );
Int ans =-1;
For (int I = 1; I <= n; I ++)
{
Int r = bin (I, I + 1, n );
Int k =-1;
If (r> I) k = rmqmax (I, r );
If (w [k]> w [I])
Ans = max (ans, k-I );
}
If (ans <1) ans =-1;
Printf ("% d \ n", ans );
}
Return 0;
}

# Include <iostream>
# Include <algorithm>
# Include <cstring>
# Include <string>
# Include <cstdio>
# Include <cmath>
# Include <queue>
# Include <map>
# Include <set>
# Define eps 1e-5
# Define maxn55555
# Define MAXM 111111
# Define INF 1000000000
Using namespace std;
Int mi [MAXN] [17], mx [MAXN] [17], w [MAXN];
Int Log [MAXN];
Int n;
Void rmqinit (int n)
{
For (int I = 1; I <= n; I ++) mi [I] [0] = mx [I] [0] = I;
Int m = (int) (log (n * 1.0)/log (2.0 ));
For (int I = 1; I <= m; I ++)
For (int j = 1; j <= n; j ++)
{
Mx [j] [I] = mx [j] [I-1];
Mi [j] [I] = mi [j] [I-1];
If (j + (1 <(I-1) <= n)
{
If (w [mx [j] [I] <w [mx [j + (1 <(I-1)] [I-1]) mx [j] [I] = mx [j + (1 <(I-1)] [I-1];
If (w [mi [j] [I]> w [mi [j + (1 <(I-1)] [I-1]) mi [j] [I] = mi [j + (1 <(I-1)] [I-1];
}
}
}
Int rmqmin (int l, int r)
{
Int m = Log [r-l + 1];
If (w [mi [l] [m]> w [mi [r-(1 <m) + 1] [m]) return mi [r-(1 <m) + 1] [m];
Else return mi [l] [m];
}
Int rmqmax (int l, int r)
{
Int m = Log [r-l + 1];
If (w [mx [l] [m] <w [mx [r-(1 <m) + 1] [m]) return mx [r-(1 <m) + 1] [m];
Else return mx [l] [m];
}
Int bin (int x, int l, int r)
{
Int ret =-1;
While (l <= r)
{
Int m = (l + r)> 1;
If (w [x] <w [rmqmin (l, m)])
L = m + 1, ret = max (ret, m );
Else r = m-1;
}
Return ret;
}

Int main ()
{
Log [1] = 0;
For (int I = 2; I <MAXN; I ++) Log [I] = Log [I> 1] + 1;
While (scanf ("% d", & n )! = EOF)
{
For (int I = 1; I <= n; I ++) scanf ("% d", & w [I]);
Rmqinit (n );
Int ans =-1;
For (int I = 1; I <= n; I ++)
{
Int r = bin (I, I + 1, n );
Int k =-1;
If (r> I) k = rmqmax (I, r );
If (w [k]> w [I])
Ans = max (ans, k-I );
}
If (ans <1) ans =-1;
Printf ("% d \ n", ans );
}
Return 0;
}


 

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.