Submission #1312606

#TimeUsernameProblemLanguageResultExecution timeMemory
1312606syanvuExam (eJOI20_exam)C++20
0 / 100
1 ms576 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], mx[N];
int f[N];
vector<int> g[2 * N];

void upd(int i, int x){
    while(i <= n){
        f[i] = max(f[i], x);
        i += i & -i;
    }
}
int get(int i){
    int res = 0;
    while(i){
        res = max(res, f[i]);
        i -= i & -i;
    }
    return res;
}


void dnc(int l, int r, int optl, int optr, int x){
    if(l > r) return;
    int mid = (l + r) / 2;
    int cur = 0, optp = 0;
    for(int i = optl; i <= min(mid, optr); i++){
        int res = get(g[x][i] - 1) + (mid - i + 1);
        if(res > cur){
            cur = res;
            optp = i;
        }
    }
    dnc(l, mid - 1, optl, optp, x);
    dnc(mid + 1, r, optp, optr, x);
    upd(g[x][mid], cur);
}

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]);
    }
    for(int i = 1; i <= 2 * n; i++){
        g[i].push_back(0);
    }
    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; 
        g[b[i]].push_back(i);
    }
    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 l[2 * n + 1] = {}, r[2 * n + 1] = {};
    for(int i = 1; i <= n; i++){
        int x = a[i];
        while(r[x] < g[x].size() && g[x][r[x]] < R[i] && g[x][r[x]] <= i) r[x]++;
        if(g[x][r[x]] >= R[i]) r[x]--;
        while(l[x] < g[x].size() && g[x][l[x]] <= L[i]) l[x]++;
        if(l[x] > r[x] || l[x] >= g[x].size() || r[x] >= g[x].size()) continue;
        dnc(l[x], r[x], l[x], r[x], x);
    }
    cout << get(n);
}

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...