답안 #815678

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
815678 2023-08-08T18:24:40 Z VMaksimoski008 Exam (eJOI20_exam) C++14
100 / 100
132 ms 197180 KB
#include <bits/stdc++.h>

#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define rall(x) x.rbegin(), x.rend()
#define each(x, v) for(auto &x : v)
#define mp make_pair

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;

void setIO() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}

int n;
vector<int> a, b;
vector<int> L, R;

void solve_2() {
    int curr = b[0];
    int ans = 0;

    for(int i=0; i<n; i++) {
        if(a[i] != curr) continue;
        ans++;
        int j = i-1;
        while(j >= 0 && a[j] <= curr) j--, ans++;
        j = i+1;
        while(j < n && a[j] <= curr) j++, ans++;
        i = j-1;
    }

    cout << ans << '\n';
}

struct SegTree {
    int n;
    vector<int> tree;

    SegTree(int n) : n(n), tree(4*n+5, 0) {}

    void update(int u, int tl, int tr, int pos, int val) {
        if(tl == tr) {
            tree[u] = max(tree[u], val);
        } else {
            int tm = (tl + tr) / 2;
            if(pos <= tm)
                update(2*u, tl, tm, pos, val);
            else
                update(2*u+1, tm+1, tr, pos, val);
            tree[u] = max(tree[2*u], tree[2*u+1]);
        }
    }

    int query(int u, int tl, int tr, int l, int r) {
        if(tl > tr || l > tr || tl > r) return 0;
        if(l <= tl && tr <= r) return tree[u];
        int tm = (tl + tr) / 2;
        return max(
          query(2*u, tl, tm, l, r),
          query(2*u+1, tm+1, tr, l, r)  
        );
    }

    void update(int pos, int val) {
        update(1, 0, n-1, pos, val);
    }

    int query(int l, int r) {
        return query(1, 0, n-1, l, r);
    }
};

int32_t main() {
    setIO();

    cin >> n;
    a.resize(n);
    b.resize(n);
    L.resize(n);
    R.resize(n);
    vector<int> temp(n+2, 0);
    set<int> diff;
    map<int, int> comp;

    for(int i=0; i<n; i++) {
        cin >> a[i];
        comp[a[i]] = i;
        temp[i+1] = a[i];
    }

    for(int i=0; i<n; i++) {
        cin >> b[i];
        diff.insert(b[i]);
    }

    if(sz(diff) == 1) {
        solve_2();
        return 0;
    }

    temp[0] = INT_MAX;
    temp[n+1] = temp[0];

    stack<int> st;
    st.push(0);

    for(int i=1; i<=n; i++) {
        while(temp[st.top()] <= temp[i])
            st.pop();
        L[i-1] = st.top() - 1;
        st.push(i);
    }

    while(!st.empty()) st.pop();

    st.push(n+1);
    for(int i=n; i>=1; i--) {
        while(temp[st.top()] <= temp[i])
            st.pop();
        R[i-1] = st.top() - 1;
        st.push(i);
    }
    
    if(n <= 5004) {
        vector<vector<ll> > dp(n+5, vector<ll>(n+5, 0));
        for(int i=1; i<=n; i++) {
            for(int j=1; j<=n; j++) {
                if(a[i-1] == b[j-1] && L[i-1] < j-1 && j-1 < R[i-1])
                    dp[i][j] = dp[i][j-1] + 1;
                dp[i][j] = max(dp[i][j], dp[i-1][j]);
                dp[i][j] = max(dp[i][j], dp[i][j-1]);
            }
        }

        cout << dp[n][n] << '\n';
        return 0;
    }

    SegTree tree(n+1);
    for(int i=0; i<n; i++) {
        int ans = 0;
        if(!comp.count(b[i])) continue;
        if(L[comp[b[i]]] < i && R[comp[b[i]]] > i) {
            ans = tree.query(0, comp[b[i]]);
            tree.update(comp[b[i]], ans+1);
        }
    }

    cout << tree.query(0, n-1) << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 4 ms 596 KB Output is correct
3 Correct 23 ms 6228 KB Output is correct
4 Correct 10 ms 2388 KB Output is correct
5 Correct 50 ms 6920 KB Output is correct
6 Correct 9 ms 2260 KB Output is correct
7 Correct 12 ms 2260 KB Output is correct
8 Correct 36 ms 5244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 2260 KB Output is correct
3 Correct 19 ms 32008 KB Output is correct
4 Correct 95 ms 181628 KB Output is correct
5 Correct 120 ms 197024 KB Output is correct
6 Correct 101 ms 197044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 197180 KB Output is correct
2 Correct 38 ms 4692 KB Output is correct
3 Correct 92 ms 11472 KB Output is correct
4 Correct 95 ms 13316 KB Output is correct
5 Correct 132 ms 13240 KB Output is correct
6 Correct 125 ms 12216 KB Output is correct
7 Correct 109 ms 13036 KB Output is correct
8 Correct 86 ms 11988 KB Output is correct
9 Correct 107 ms 12820 KB Output is correct
10 Correct 87 ms 12748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 0 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 2260 KB Output is correct
9 Correct 19 ms 32008 KB Output is correct
10 Correct 95 ms 181628 KB Output is correct
11 Correct 120 ms 197024 KB Output is correct
12 Correct 101 ms 197044 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 0 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 5 ms 8276 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 132 ms 196756 KB Output is correct
25 Correct 111 ms 196684 KB Output is correct
26 Correct 108 ms 196680 KB Output is correct
27 Correct 108 ms 196608 KB Output is correct
28 Correct 100 ms 196908 KB Output is correct
29 Correct 101 ms 196840 KB Output is correct
30 Correct 104 ms 196832 KB Output is correct
31 Correct 100 ms 196860 KB Output is correct
32 Correct 100 ms 196896 KB Output is correct