제출 #1312612

#제출 시각아이디문제언어결과실행 시간메모리
1312612syanvuExam (eJOI20_exam)C++20
88 / 100
1097 ms16596 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 = 1e5 + 1, 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;
}

/*
mx[l - 1] + (r - l + 1)
mx[l - 1] - l + (r + 1);
*/

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]) r[x]++;
        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;
        int mx = -inf;
        vector<int> v;
        for(int j = l[x]; j <= r[x]; j++){
            mx = max(mx, get(g[x][j] - 1) - j);
            upd(g[x][j], mx + j + 1);
        }
    }
    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...