题意: 中文
思路: 原本以为需要用到一些超级厉害的脑洞或者黑科技,仔细想了想其实就是一个栈的模拟。。真是失望。。
讲一下思路把,就是我们把前面的数字扔到栈里,之后和我们现在遇到的数字比较,如果差值为1的话,我们就看那个大,如果栈里的大,那么我们就把栈里面的数字pop出来,之后在和栈顶比较,直到差值不为1的时候,如果当前数字大的话我们就直接计数++,移到下一个就好了上代码把
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int a[maxn];
int main()
{
int n;
scanf("%d",&n);
stack<int>S;
for(int i = 0 ; i < n ; i++)
{
cin>>a[i];
}
S.push(a[0]);
int pos = 1;
int ans = 0 ;
for(int i = 1 ; i < n ; i++)
{
if(S.empty())
{
S.push(a[i]);
continue;
}
int t = S.top();
if(t - a[i] == -1)
{
ans++;continue;
}
if(t - a[i] == 1)
{
i--;
S.pop();
ans++;
}
else S.push(a[i]);
}
cout<<ans<<endl;
}