オリジナルタイトル住所
問題解決のためのアイデア
最初の単調なスタックは、2つの配列を取得[]、[] Bに格納された左自身のインデックス番号のそれぞれの最初の数よりも小さいです。次いで、1〜n個のサイクルのため、比較[i]とB [i]は等しくないループが終了された場合に、同じであるこの時点で最終的な答えに等しいです。
前者は、Lは〜、1〜n-1の条件は、次に1〜n個の実現可能性を決定する、満たされていることを前提とN満足両方か否かを判断する、[N] <[n]の場合にされたときに 、B がL = bの[n]は次いで、RBQ配列は明らか2、[N]> B [成り立たない、B [n]は、n個のシーケンスである n]は同じように。[n]は[n]は、LをA> Bに等しい場合 [n]は、それが理由N、正面には影響がない最小の数のことが可能であった場合であるRBQ場合N、L <= N、明確に確立しますA。
コードは以下の通りです
#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;
}