Monotonous stack entry

What stack is a monotonous?

Divided into monotonically increasing and monotonically decreasing stack. (The element to increment or decrement of the stack)

For example:
The current monotonously increases within the stack elements [2,4], after the stack of elements (3),
in order to maintain monotonicity, the element (4) out of the stack

\ [[2,4] - the stack (3) - the stack (4) - [1,2,3] - the stack (0) - the stack (1) (2) (3) - [0 ] \]

Monotonous increment stack major role:

Each element in the sequence into monotone stack can be maintained in the \ (O (n) \) time complexity
when the interval for each element is determined maximum / minimum value, the influence range is the interval [left, right].

Monotonically increasing stack for the minimum scope

Stack monotone decreasing the scope of selecting the maximum value

\ (Example: the sequence {1,2,3,2,1} \)

    1 2 3 2 1
        口
      口口口
    口口口口口
    0 1 2 3 4

Monotonous descending stack to obtain

Maximum Interval [left, right]
1 [0,0]
2 [0,1]
3 [0,4]
2 [3,4]
1 [4,4]

Maintenance monotonous stack:

Here we take a monotonically increasing stack, for example, minimum sphere of influence

We require the subscript (index) pushed onto the stack. For coding convenience, we use an array of monotonically added last stack -INF (a minimum value), to facilitate the subsequent stack.

Sequence becomes \ ({1,2,3,2,1, -INF} \ )

i To stack the height [i] Changes stack After the change stack
0 1 push(0) [0]
1 2 push(1) [0,1]
2 3 push(2) [0,1,2]
3 2 pop(2),push(3) [0,1,3]
4 1 pop(3),pop(1),push(4) [0,4]
5 -INF pop(0),push(4) []

[Left, right] in right:

If the element height [i] pop from the stack the element is described on the right side of the scope of the minimum value so far.

[left,right]中的left:

Because elements within the stack is incremented monotonically, then pop the stack, the stack element height [s.top ()] is not greater than the element of pop. Therefore, the scope of the left index +1 elements after the top of the stack is pop, pop to note here after stack case is empty, because then pop the stack is empty, indicating that no element is smaller than pop out the elements that the pop-out element affects all elements of its left end.

//单调递增栈
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long int LL;
const int MAXN = 1e6 + 1;
LL height[MAXN];
int N;

void solve(){
    height[N] = -INF;
    stack<int> s;
    for(int i=0;i<=N;i++){
        while(!s.empty() && height[s.top()] > height[i]){
            int cur = s.top();
            s.pop();
            int _left = s.empty()?0:s.top()+1;
            int _right = i-1;
            cout << height[cur] << " " << _left << " " << _right << endl;
        }
        s.push(i);
    }
}

int main() {
    cin >> N;
    for(int i=0;i<N;i++) cin >> height[i];
    solve();
    return 0;
}
//单调递减栈
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long int LL;
const int MAXN = 1e6 + 1;
LL height[MAXN];
int N;

void solve(){
    height[N] = INF;
    stack<int> s;
    for(int i=0;i<=N;i++){
        while(!s.empty() && height[s.top()] < height[i]){
            int cur = s.top();
            s.pop();
            int _left = s.empty()?0:s.top()+1;
            int _right = i-1;
            cout << height[cur] << " " << _left << " " << _right << endl;
        }
        s.push(i);
    }
}

int main() {
    cin >> N;
    for(int i=0;i<N;i++) cin >> height[i];
    solve();
    return 0;
}

template

void solve(){
    //单调递增栈 -INF,递减 INF
    height[N] = -INF;
    stack<int> s;
    for(int i=0;i<=N;i++){
        //单调递增栈 >,递减 <,等号看题目
        while(!s.empty() && height[s.top()] > height[i]){
            int cur = s.top();
            s.pop();
            int _left = s.empty()?0:s.top()+1;
            int _right = i-1;
            cout << height[cur] << " " << _left << " " << _right << endl;
        }
        s.push(i);
    }
}

Guess you like

Origin www.cnblogs.com/--zz/p/11247874.html