题意:
给两个长度为n的数组a,b
求一个最大的p,使得:
a,b数组[1,p]所有子区间中最小值下标相同
思路:
两个栈维护两个数组每个点左边比他小的数的位置
当两个栈的大小不一样的时候说明下标不同,不满足,否则满足
code:
//https://ac.nowcoder.com/acm/contest/881/A
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std;
const int maxm=1e5+5;
int a[maxm],b[maxm];
int n;
signed main(){
while(scanf("%d",&n)!=EOF){
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++){
scanf("%d",&b[i]);
}
stack<int>s,ss;
int ans=0;
for(int i=1;i<=n;i++){
while(!s.empty()&&s.top()>a[i]){
s.pop();
}
s.push(a[i]);
while(!ss.empty()&&ss.top()>b[i]){
ss.pop();
}
ss.push(b[i]);
if(s.size()==ss.size()){
ans++;
}else{
break;
}
}
printf("%d\n",ans);
}
return 0;
}