理解
- 栈中的某一类元素具有单调性,单调递减或递增
用处
- 维护元素单调性的一种数据结构
#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;
}