题目大意
两种操作:
1.向栈中加入x元素。
2.删除栈顶元素。
需要修改几次顺序能使序列单调。
题解
模拟栈。
add操作向栈中加入元素。
remove操作若栈顶元素等于remove次数,弹出栈顶。否则把栈清空,答案加1.
(博主实在太弱啦,竟然想用单调栈和最长上升子序列)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
stack<int> s;
int read()
{
char ch=getchar();int f=0;
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9')
{f=(f<<1)+(f<<3)+ch-'0';
ch=getchar();}
return f;
}
int now=1,ans=0;
int main()
{
int n=read();char ss[15];
for(int i=1;i<=2*n;i++)
{
scanf("%s",ss);
if(ss[0]=='a')
{
int x=read();
s.push(x);
}
else
{
if(!s.empty())
{
if(s.top()==now)
{
s.pop();
}
else
{
ans++;
while(!s.empty())
s.pop();
}
}
now++;
}
}
cout<<ans;
}