POJ 2452 (RMQ + binary)
Sticks Problem
| Time Limit:6000 MS |
|
Memory Limit:65536 K |
| Total Submissions:10141 |
|
Accepted:2682 |
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
Question: Give a sequence with the length of n where Si ~ Sj (1 <= I <j <= n) is its subsequence. Q: Meet Si ~ The maximum value of j-I in any sequence with a number greater than Si and less than Sj.
Solution: I never thought about brute force at the beginning, but in fact, it's okay to use brute force .....
However, my method is to use RMQ to process the most value of the interval, and then scan it over, and use two decimal points for each number to find the maximum range that can be formed by this number. (Assume that the starting point is I first) The two binary functions are as follows:
First time: Find the maximum range location where I can be used as the minimum value (in fact, this can be processed using a monotonous stack) Second Time: Find the maximum position of this interval within the interval first time.
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define PI acos (-1.0) # define eps 1e-8const int inf = (1 <30)-10; using namespace std; const int maxx = 50000 + 10; int n; int a [maxx]; int dpmax [maxx] [20]; int dpmin [maxx] [20]; int mn [maxx]; inline void init_rmq (int a [], int n) {// construct rmq mn [0] =-1; for (int I = 1; I <= n; ++ I) {if (I & (I-1) = 0) {mn [I] = mn [I-1] + 1 ;} else {mn [I] = mn [I-1];} dpmax [I] [0] = a [I]; dpmin [I] [0] = a [I];} for (int j = 1; j <= mn [n]; ++ j) {for (int I = 1; I + (1 <
> 1; if (min_rmq (I, mid) = a [I]) {l = mid;} else {r = mid ;}} int ll = I; int rr = l; if (l! = I) {int temp = max_rmq (ll, rr); while (ll + 1! = Rr) {int mid = (ll + rr)> 1; if (max_rmq (I, mid) <temp) {ll = mid ;} else {rr = mid ;}}if (rr-I> ans) ans = rr-I;} if (ans) printf (% d, ans ); else printf (-1);} return 0;}/** 71 2 3 7 4 5 6 */