Original title Address
Problem-solving ideas
First monotonous stack get two sequences is smaller than the first number of each of the left own index number, stored in a [], b []. Then the order of 1 ~ n cycles, comparing a [i] and b [i] are equal to, if not equal loop is exited, at this time is equal to a final answer.
When the former is assumed that 1 ~ n-1 conditions have been satisfied, then determining the feasibility of 1 ~ n, l ~ n is determined whether both satisfied, if a [n] <[n] b, then when l = b [n] , RBQ sequence is b [n], a sequence of n, obviously does not hold 2, a [n]> b [ n] the same way. When a [n] is equal to b [n], l> a [n] are RBQ case n, l <= n when it has been possible because of the smallest number n, there is no effect on the front, clearly established a.
code show as below
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
inline int read(){
int res = 0, w = 0; char ch = 0;
while(!isdigit(ch)){
w |= ch == '-', ch = getchar();
}
while(isdigit(ch)){
res = (res << 3) + (res << 1) + (ch ^ 48);
ch = getchar();
}
return w ? -res : res;
}
const int N = 100005;
struct T{
int val, i;
T(int val, int i): val(val), i(i){}
};
stack<T> sta1, sta2;
int a[N], b[N];
int main()
{
int n;
while(scanf("%d", &n) != EOF){
for(int i = 1; i <= n; i ++){
int x = read();
while(!sta1.empty() && sta1.top().val > x)
sta1.pop();
if(!sta1.empty())
a[i] = sta1.top().i;
else
a[i] = 0;
sta1.push(T(x, i));
}
for(int i = 1; i <= n; i ++){
int x = read();
while(!sta2.empty() && sta2.top().val > x)
sta2.pop();
if(!sta2.empty())
b[i] = sta2.top().i;
else
b[i] = 0;
sta2.push(T(x, i));
}
while(!sta1.empty())
sta1.pop();
while(!sta2.empty())
sta2.pop();
int ans = 0;
for(int i = 1; i <= n; i ++){
if(a[i] == b[i])
ans = i;
else
break;
}
printf("%d\n", ans);
}
return 0;
}