Submission #1312535

#TimeUsernameProblemLanguageResultExecution timeMemory
1312535syanvuExam (eJOI20_exam)C++20
0 / 100
182 ms8444 KiB
// #pragma optimize ("g",on)
// #pragma GCC optimize ("inline")
// #pragma GCC optimize ("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC optimize ("03")
#include <bits/stdc++.h>

#define pb push_back
#define SS ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
#define int long long
#define all(v) v.begin(),v.end()
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const int N = 5001, inf = 1e9, mod = 998244353;

int n;
int a[N], b[N];
int t[4 * N], add[4 * N];

void upd(int v, int tl, int tr, int pos, int x){
    if(tl == tr){
        t[v] = max(t[v], x);
        return;
    }
    int mid = (tl + tr) / 2;
    if(mid >= pos) upd(v * 2, tl, mid, pos, x);
    else upd(v * 2 + 1, mid + 1, tr, pos, x);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
}
int get(int v, int tl, int tr, int l, int r){
    if(tl > r || l > tr || l > r) return 0;
    if(tl >= l && r >= tr) return t[v];
    int mid = (tl + tr) / 2;
    return max(get(v * 2, tl, mid, l, r), get(v * 2 + 1, mid + 1, tr, l, r));
}

void solve(){
    cin >> n;
    vector<int> v;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        v.push_back(a[i]);
    }
    for(int i = 1; i <= n; i++){
        cin >> b[i];
        v.push_back(b[i]);
    }
    sort(all(v));
    v.erase(unique(all(v)), v.end());
    for(int i = 1; i <= n; i++){ 
        a[i] = lower_bound(all(v), a[i]) - v.begin() + 1;
        b[i] = lower_bound(all(v), b[i]) - v.begin() + 1; 
    }
    stack<int> st;
    int R[n + 1] = {}, L[n + 1] = {};
    for(int i = n; i >= 1; i--){
        while(st.size() && a[st.top()] <= a[i]) st.pop();
        if(st.size()) R[i] = st.top();
        else R[i] = n + 1;
        st.push(i);
    }
    while(st.size()) st.pop();
    for(int i = 1; i <= n; i++){
        while(st.size() && a[st.top()] <= a[i]) st.pop();
        if(st.size()) L[i] = st.top();
        st.push(i);
    }
    int ans[n + 1][n + 1] = {};
    for(int i = 1; i <= n; i++){
        int cnt[n + 1] = {}, mx = 0;
        for(int j = i; j <= n; j++){
            cnt[b[j]]++;
            mx = max(mx, a[j]);
            ans[i][j] = cnt[mx];
        }
    }
    for(int i = 1; i <= n; i++){
        for(int l = i; l > L[i]; l--){
            for(int r = i; r < R[i]; r++){
                upd(1, 1, n, r, ans[l][r] + get(1, 1, n, 1, l - 1));
            }
        }
    }
    cout << t[1];
}

signed main(){
    SS
    // freopen("trains.in", "r", stdin);
    // freopen("trains.out", "w", stdout);

    int t = 1;
    // cin >> t;
    while(t--){
        solve();
    }
}
#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...