链接:https://ac.nowcoder.com/acm/contest/881/A
来源:牛客网
做法:维护两个单调递增栈,如果两个单调栈内元素不等,那么就到此为止,输出长度
实际上是找出当前元素左边的第一个小于等于它的值的位置,如果不同,就不行了。这个思想找几个样例手动模拟一下就明白了。
这个题可以改一下,把最小值换成最大值,做法也应该换成维护两个单调递减栈
#include <bits/stdc++.h>
using namespace std;
const int maxN=1e5+5;
int N,a[maxN],b[maxN],ans;
stack<int> st1,st2;
int main()
{
while(~scanf("%d",&N))
{
ans=N;
while(!st1.empty())st1.pop();
while(!st2.empty())st2.pop();
for(int i=1;i<=N;i++)scanf("%d",&a[i]);
for(int i=1;i<=N;i++)scanf("%d",&b[i]);
for(int i=1;i<=N;i++)
{
while(!st1.empty()&&st1.top()>a[i])st1.pop();
st1.push(a[i]);
while(!st2.empty()&&st2.top()>b[i])st2.pop();
st2.push(b[i]);
if(st1.size()!=st2.size())
{
ans=i-1;
break;
}
}
printf("%d\n",ans);
}
return 0;
}