7-31 单调栈和单调队列总结
tanruiqing · · 个人记录
前言
我发誓下一次再也不不注意数据范围了!!!
为什么?
没为什么。。
请移步至 T3 的【数据范围】一处。。
得分
别说了。。
T1
思路:
因为题目说要求位置
所以,我们需要优化。
显然,枚举法的内层循环可以优化掉。
用什么优化呢?
观察题目,题目说要找到每一个位置
首先考虑二分,二分什么?二分每一个位置
其次考虑单调栈,因为需要找到第一个小于的数,所以我们需要维护一个单调递增的序列。因为是小于的数,所以我们要判栈顶是否大于目前数字,大于说明不满足条件,弹出栈。直到第一个小于目前数字的数。需不需要判等于?这是一个值得思考的点。因为题目说会有重复的数字,所以我们找到了栈顶第一个等于目前数字的数也需要弹出。
答案如何统计?
我们可以用两个数组
::::::warning[注意]
在统计答案的时候,需要判断一下栈是否为空,如果为空并且你调用了 st.top(),你就可以获得 RE。
| 其次,答案数组需要在最前面全部赋值为 |
|---|
然后我们可以写出这样一段代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int a[500005];
int l[500005],r[500005];
int read(){
int cnt=0,f=0;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=!f;
c=getchar();
}
while(c>='0'&&c<='9'){
cnt=cnt*10+c-'0';
c=getchar();
}
return ((f==1)?-cnt:cnt);
}
struct node{
int a[500005],l;
int top(){
return a[l];
}
void pop(){
l--;
}
void push(int x){
a[++l]=x;
}
int size(){
return l;
}
bool empty(){
return l==0;
}
}st;
signed main(){
cin.tie(0)->sync_with_stdio(false);
cout.tie(0)->sync_with_stdio(false);
n=read();
for(int i=1;i<=n;i++){
a[i]=read();
}
for(int i=1;i<=n;i++)l[i]=-1,r[i]=-1;
for(int i=1;i<=n;i++){
while(!st.empty()&&a[st.top()]>=a[i])st.pop();
if(!st.empty())r[i]=st.top();
st.push(i);
}
for(int i=n;i>=1;i--){
while(!st.empty()&&a[st.top()]>=a[i])st.pop();
if(!st.empty())l[i]=st.top();
st.push(i);
}
for(int i=1;i<=n;i++){
cout<<r[i]<<" "<<l[i]<<'\n';
}
return 0;
}
然后你可以获得
因为我们跑了两边统计答案的循环,但是用是同一个栈,所以……
::::::warning[打开这里] 多测不清空,爆零两行泪…… ::::::
AC 代码:
不知为什么,这题快读被卡了。。
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int a[500005];
int l[500005],r[500005];
int read(){
int cnt=0,f=0;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=!f;
c=getchar();
}
while(c>='0'&&c<='9'){
cnt=cnt*10+c-'0';
c=getchar();
}
return ((f==1)?-cnt:cnt);
}
struct node{
int a[500005],l;
int top(){
return a[l];
}
void pop(){
l--;
}
void push(int x){
a[++l]=x;
}
int size(){
return l;
}
bool empty(){
return l==0;
}
}st;
signed main(){
cin.tie(0)->sync_with_stdio(false);
cout.tie(0)->sync_with_stdio(false);
// n=read();
cin>>n;
for(int i=1;i<=n;i++){
// a[i]=read();
cin>>a[i];
}
for(int i=1;i<=n;i++)l[i]=-1,r[i]=-1;
for(int i=1;i<=n;i++){
while(!st.empty()&&a[st.top()]>=a[i])st.pop();
if(!st.empty())r[i]=st.top();
st.push(i);
}
while(!st.empty())st.pop();
for(int i=n;i>=1;i--){
while(!st.empty()&&a[st.top()]>=a[i])st.pop();
if(!st.empty())l[i]=st.top();
st.push(i);
}
for(int i=1;i<=n;i++){
cout<<r[i]<<" "<<l[i]<<'\n';
}
return 0;
}
T3
思路:
我们看到“区间”和“最大/小值”这两个词就要想起一个东西——单调队列(滑动窗口),我们可以每次框出一个长度为
- 后缀最大值的定义:对于一个数
a_i ,在选定的区间[L\sim R] 之内每一个a_j(L\le i<j\le R) 都有a_j<a_i 。
通过定义,我们可以得出如果这个数和队首的元素的数字一样,也需要弹出。
AC 代码
#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
int read(){
int cnt=0,f=0;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=!f;
c=getchar();
}
while(c>='0'&&c<='9'){
cnt=cnt*10+c-'0';
c=getchar();
}
return ((f==1)?-cnt:cnt);
}
struct node{
int a[1000005],l;
int top(){
return a[l];
}
void pop(){
l--;
}
void push(int x){
a[++l]=x;
}
int size(){
return l;
}
bool empty(){
return l==0;
}
}st;
deque<int> q;
int n,m;
int a[1000005];
signed main(){
cin.tie(0)->sync_with_stdio(false);
cout.tie(0)->sync_with_stdio(false);
n=read();
m=read();
for(int i=1;i<=n;i++){
a[i]=read();
}
for(int i=1;i<=n;i++){
while(!q.empty()&&q.front()+m<=i)q.pop_front();
while(!q.empty()&&a[i]>=a[q.back()])q.pop_back();
//死因:在判 a[i]>=a[q.back()] 时写成了 >。
q.push_back(i);
if(i>=m){
cout<<q.size()<<'\n';
}
}
return 0;
}
T5
思路:
首先:这道题的宽度没有用。
为什么?
因为对于同一高度的建筑,我们不可能将它中间分开用两个不同的区间覆盖(由此我们可以推出:如果有两栋高度一样的楼,我们可以将其合并)。
其次,我们可以观察出:如果有一栋楼高为
然后,我们可以维护一个单调递增的栈,在栈顶元素大于目前元素时弹出。如果弹出完栈顶元素和目前元素一样,那么统计答案。
当然,在栈为空的时候也需要累加答案。因为我们需要一张海报来覆盖后面的。
AC 代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int read(){
int cnt=0,f=0;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=!f;
c=getchar();
}
while(c>='0'&&c<='9'){
cnt=cnt*10+c-'0';
c=getchar();
}
return ((f==1)?-cnt:cnt);
}
struct node{
int a[500005],l;
int top(){
return a[l];
}
void pop(){
l--;
}
void push(int x){
a[++l]=x;
}
int size(){
return l;
}
bool empty(){
return l==0;
}
}st;
deque<int> q;
int n;
int x,a[500005];
signed main(){
cin.tie(0)->sync_with_stdio(false);
cout.tie(0)->sync_with_stdio(false);
n=read();
for(int i=1;i<=n;i++){
x=read();a[i]=read();
}
int ans=0;
for(int i=1;i<=n;i++){
while(!st.empty()&&a[st.top()]>a[i]){
st.pop();
}
if(st.empty())ans++;
else if(a[i]!=a[st.top()])ans++;
st.push(i);
}
cout<<ans;
return 0;
}
T6
思路:
我们可以维护一个队列,每次将一个珠子加入队列,如果我们已经拥有了
死因:push 珠子的时候外面多加了一层 while,在输入时两个变量放反了导致枚举的是位置。
AC 代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int read(){
int cnt=0,f=0;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=!f;
c=getchar();
}
while(c>='0'&&c<='9'){
cnt=cnt*10+c-'0';
c=getchar();
}
return ((f==1)?-cnt:cnt);
}
deque<int> q;
struct node{
int pos,id;
};
vector<node> e;
int n,m;
int vis[1000005];
int cnt;
int maxx;
bool cmp(node a,node b){
return a.pos<b.pos;
}
signed main(){
cin.tie(0)->sync_with_stdio(false);
cout.tie(0)->sync_with_stdio(false);
cin>>n>>m;
for(int i=1;i<=m;i++){
int t;
cin>>t;
while(t--){
int x;
cin>>x;
e.push_back(node{x,i});
}
}
int minn=1e18;
sort(e.begin(),e.end(),cmp);
for(int i=0;i<n;i++){
q.push_back(i);
int id=e[i].id;
if(vis[id]==0){
cnt++;
}
vis[id]++;
while(!q.empty()&&cnt>=m){
int j=q.front();
q.pop_front();
int id=e[j].id;
vis[id]--;
if(vis[id]==0){
cnt--;
minn=min(minn,e[i].pos-e[j].pos);
}
}
}
cout<<minn;
return 0;
}
T7
死因:栈用成队列,100->28。
思路:
我们可以用一个 struct 记录每个身高为
然后,我们维护一个单调递减的栈,然后将比目前这个人身高矮的人 pop 掉,但是此时他们可以互相看见,所以需要累加答案。
当然,如果弹掉的那个人和目前的人的升高一样的话,我们就需要将这个身高的人数增加。
最后,如果栈的元素个数不为
最后加入当前元素即可。
AC 代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int read(){
int cnt=0,f=0;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=!f;
c=getchar();
}
while(c>='0'&&c<='9'){
cnt=cnt*10+c-'0';
c=getchar();
}
return ((f==1)?-cnt:cnt);
}
int n;
int a[500005];
struct node{
int x,y;
};
stack<node> q;
signed main(){
cin.tie(0)->sync_with_stdio(false);
cout.tie(0)->sync_with_stdio(false);
n=read();
for(int i=1;i<=n;i++){
a[i]=read();
}
int ans=0;
for(int i=1;i<=n;i++){
node b=node{a[i],1};
while(!q.empty()&&q.top().x<=a[i]){
ans+=q.top().y;
if(q.top().x==a[i]){
b.y+=q.top().y;
}
q.pop();
}
if(!q.empty())ans++;
q.push(b);
}
cout<<ans;
return 0;
}