제출 #646909

#제출 시각아이디문제언어결과실행 시간메모리
646909LeonaRagingExam (eJOI20_exam)C++14
77 / 100
193 ms98660 KiB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define ll long long
#define pb push_back
#define db(val) "[" #val " = " << (val) << "] "

const ll mod = 1e9 + 7;
const int maxn = 1e5 + 4;
const int INF = 1e9;
const int N = 5005;

int n, a[maxn], b[maxn], L[maxn], R[maxn], dp[N][N];
set<int> mySet;

void Sub2() {
    int res = 0, pre = 0;
    for (int i = 1; i <= n; i++)
        if (a[i] == b[i]) {
            res += R[i] - max(pre, L[i]) - 1;
            pre = R[i];
            i = R[i] - 1;
        }
    cout << res;
}

void Sub4() {

}

void Sub6() {
    int res = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) {
            if (a[i] == b[j]) {
                if (L[i] < j && j < R[i])
                    dp[i][j] = dp[i][j - 1] + 1;
            }
            dp[i][j] = max({dp[i][j], dp[i][j - 1], dp[i - 1][j]});
            res = max(res, dp[i][j]);
        }
    cout << res << '\n';
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen(".INP", "r", stdin);
    //freopen(".OUT", "w", stdout);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++) {
        cin >> b[i];
        mySet.insert(b[i]);
    }
    stack<int> st;
    st.push(0);
    a[0] = a[n + 1] = INF + 1;
    for (int i = 1; i <= n; i++) {
        while (a[st.top()] <= a[i])
            st.pop();
        L[i] = st.top();
        st.push(i);
    }
    stack<int>().swap(st);
    st.push(n + 1);
    for (int i = n; i >= 1; i--) {
        while (a[st.top()] <= a[i])
            st.pop();
        R[i] = st.top();
        st.push(i);
    }
    if (mySet.size() == 1) Sub2();
    else if (n > 5000) Sub4();
    else { 
        Sub6();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...