单调栈


理解

  • 栈中的某一类元素具有单调性,单调递减或递增

用处

  • 维护元素单调性的一种数据结构
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#include <time.h>
#include <stdlib.h>

using namespace std;
stack <int> s; //构建一个单调递增的栈 
#define n 100
int main() {
    srand(time(0));
    s.push(-1); 
      for (int i = 1; i <= n; i++) {
          int a = rand() % 100;
          while (s.top() > a) s.pop();
          s.push(a);
      }
      while (s.size() != 1) {
          cout << s.top() << endl;
          s.pop();  
    }
  return 0;
}

文章作者: Axieyun
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Axieyun !
评论
评论
  目录