#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define ins insert
#define pb push_back
#define F first
#define S second
const int N = 2e5+4, M = 5e5 + 7;
const int mod = 1e9 + 7;
int a[N], b[N], c[N], pf[N], sf[N];
void solve(){
    int n;
    cin>>n;
    for(int i = 1; i <= n; i++){ cin>>a[i]; c[i] = a[i];}
    for(int i = 1; i <= n; i++) cin>>b[i];
    sort(c + 1, c + n + 1);
    for(int i = 1; i <= n; i++){
        if(c[i] == c[i - 1]) continue;
        for(int j = 1; j <= n; j++){
            if(a[j] != c[i]) continue;
            int l = j - 1, r = j + 1;
            while(l >= 1 && a[l] < a[j]) l--;
            l++;
            while(r <= n && a[r] < a[j]) r++;
            r--;
            pf[j] = sf[j] = 0;
            for(int k = j; k >= l; k--){
                if(k != j) sf[k] = sf[k + 1];
                if(b[k] == c[i]) sf[k]++;
                else if(a[k] == b[k]) sf[k]--;
            }
            for(int k = j; k <= r; k++){
                if(k != j) pf[k] = pf[k - 1];
                if(b[k] == c[i]) pf[k]++;
                else if(a[k] == b[k]) pf[k]--;
            }
            int mx = 0, mx1 = 0;
            for(int k = j; k >= l; k--){
                if(sf[k] > sf[mx]) mx = k;
            }
            for(int k = j; k <= r; k++){
                if(pf[k] > pf[mx1]) mx1 = k;
            }
            if(sf[mx] + pf[mx1] - pf[j] > 0){
                for(int k = mx; k <= mx1; k++){
                    a[k] = c[i];
                }
            }
        }
    }
    int ans = 0;
    for(int i = 1; i <= n; i++) if(a[i] == b[i]) ans++;
    cout<<ans<<'\n';
}
main(){
    ios_base :: sync_with_stdio(false);
    cin.tie(nullptr); 
    int t = 1;
    //cin>>t; 
    for(int i = 1; i <= t; i++){
        //cout<<"Case "<<i<<": ";
        solve();
    }
}
컴파일 시 표준 에러 (stderr) 메시지
exam.cpp:56:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   56 | main(){
      | ^~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |