自己菜的一批,居然有既然能数目不一样的,以后就算在找也是一定有前面那一段是不合法的
用单调栈会快很多,怎么解释呢,看代码吧。
AC代码:
#include <bits/stdc++.h>
#include<stack>
using namespace std;
typedef long long ll;
const int N=1e5+5;
int s1[N],s2[N];
int ans;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n;
s1[0]=0;
s2[0]=0;
while(cin>>n){
stack<int> st1;
stack<int> st2;
for(int i=1;i<=n;i++)
cin>>s1[i];
for(int i=1;i<=n;i++)
cin>>s2[i];
st1.push(s1[1]);
st2.push(s2[1]);
for(int i=2;i<=n;i++)
{
while(!st1.empty()&&st1.top()>s1[i]){
st1.pop();
}
st1.push(s1[i]);
while(!st2.empty()&&st2.top()>s2[i]){
st2.pop();
}
st2.push(s2[i]);
if(st1.size()!=st2.size())
{
cout<<i-1<<endl;
break;
}
#include<stack>
using namespace std;
typedef long long ll;
const int N=1e5+5;
int s1[N],s2[N];
int ans;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n;
s1[0]=0;
s2[0]=0;
while(cin>>n){
stack<int> st1;
stack<int> st2;
for(int i=1;i<=n;i++)
cin>>s1[i];
for(int i=1;i<=n;i++)
cin>>s2[i];
st1.push(s1[1]);
st2.push(s2[1]);
for(int i=2;i<=n;i++)
{
while(!st1.empty()&&st1.top()>s1[i]){
st1.pop();
}
st1.push(s1[i]);
while(!st2.empty()&&st2.top()>s2[i]){
st2.pop();
}
st2.push(s2[i]);
if(st1.size()!=st2.size())
{
cout<<i-1<<endl;
break;
}
if(i==n)
cout<<n<<endl;
}
}
return 0;
}
cout<<n<<endl;
}
}
return 0;
}