LA 6531 Go up the Ultras monotonous stack + RMQ
Question: I am too tired to speak .. There are N peaks (N <= 10 ^ 5), and each mountain has a height h, corresponding to each mountain has a D value, each mountain has a strict ratio to all others
Its high mountain passes through a minimum value (Valley), and d represents the difference between h and the maximum values of these values (if there is no higher mountain than it, d is its own
Height), ask how many peaks d> = 150000 meters.
Train of Thought: Maintain the first position on the left of each peak with a monotonic stack l higher than it, and the first position on the right is r higher than it. For r, we maintain a monotonic reduction from the front to the back.
Sequence. If the currently considered vertex I is higher than the element height on the top of the stack, the top element of the stack pops up and its r is set to I, until the height of the element at the top of the stack is greater than or equal to the height of I. For l,
Maintain the monotonic descent sequence from the back to the back. For each vertex, obtain the minimum value d2 of [l [I], I], and the maximum value d = max (d1, d2), and then check whether h [I]-
D> = 150000. It is output. You can use RMQ to pre-process the minimum value of a range. For details, see the code:
/*************************************** * ****************** File name: LA6531.cpp author: kereo create time: friday, February 06, 2015, 19 seconds ******************************** * ************************/# include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define deusing namespace std; typedef long ll; const int sigma_size = 26; const int N = 20; const int MAXN = 100000 + 50; const int inf = 0x3fffffff; const double eps = 1e-8; const int mod = 100000000 + 7; # define L (x) (x <1) # define R (x) (x <1 | 1) # define PII pair
# Define mk (x, y) make_pair (x), (y) int n; int dp [MAXN] [N]; struct node {int l, r, h;} p [MAXN]; stack
S; int RMQ (int l, int r) {int k = 0; while (1 <(k + 1) <= r-l + 1) k ++; return min (dp [l] [k], dp [r-(1 <
= P [I]. h) {s. push (I);} else {while (! S. empty () & p [s. top ()]. h
= 1; I --) {if (s. empty () s. push (I); else if (p [s. top ()]. h> = p [I]. h) s. push (I); else {while (! S. empty () & p [s. top ()]. h
= 150000) {if (flag) {printf ("% d", I); flag = 0;} else printf ("% d", I );}} printf ("\ n");} return 0 ;}