初识单调栈
来源:互联网 发布:spring java应用 编辑:程序博客网 时间:2024/06/11 10:48
因为网络赛的一道题,了解到了单调栈
其实,自己对数据结构的理解和认识还不是很深,知道的只是皮毛,还是需要刷题。。。
单调栈:从栈顶开始单调递增,或单调递减。
具体思路:当栈为空或输入数小于栈顶数,进栈;当栈顶不为空并且输入数大于栈顶数,出站
在大神博客的启发下,自己简单写了一个
下面附上代码:
/*递增栈*/
#include<bits/stdc++.h>
using namespace std;
int main(){
int b[1000],n;
stack <int> a;
while(!a.empty())
a.pop();
cin>>n;
for(int i=0;i<n;i++)
cin>>b[i];
for(int i=0;i<n;i++){
while(!a.empty()&&b[i]>=a.top()){
a.pop();
}
if(a.empty()||b[i]<a.top())
a.push(b[i]);
}
while(!a.empty()){
cout<<a.top()<<endl;
a.pop();
}
return 0;
}
/*递减栈*/
#include<bits/stdc++.h>
using namespace std;
int main(){
int b[1000],n;
stack <int> a;
while(!a.empty())
a.pop();
cin>>n;
for(int i=0;i<n;i++)
cin>>b[i];
for(int i=0;i<n;i++){
while(!a.empty()&&b[i]<=a.top()){
a.pop();
}
if(a.empty()||b[i]>a.top())
a.push(b[i]);
}
while(!a.empty()){
cout<<a.top()<<endl;
a.pop();
}
return 0;
}
第一次写博客,代码什么的有问题,希望大家提出宝贵意见,渴望和高手们交流心得
- 初识单调栈
- 初识单调栈
- 单调栈 单调队列
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 数据挖掘系列(1)关联规则挖掘基本概念与Aprior算法
- 构造回文数操作_网易编程
- JS URL转码escape() encodeURI() encodeURIComponent()
- Installing maven 3.3.3 on Ubuntu
- 【Java并发编程】之二:线程中断(含代码)
- 初识单调栈
- hibernate hql 使用实例
- perl亲身试验ini---使用perl读写配置文件
- 【Java并发编程】之三:线程挂起、恢复与终止的正确方法(含代码)
- C语言随笔_区分=与==
- 浅谈Hive vs. HBase
- Ubuntu下开发环境配置
- Codeforces #371(Div.2)A.Meeting of Old Friends【模拟+思维】
- ubuntu远程桌面无法修改ip地址,(32) Not authorized to control networking