답안 #806231

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
806231 2023-08-04T05:23:13 Z josiftepe Exam (eJOI20_exam) C++14
100 / 100
256 ms 99500 KB
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <stack>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
const int INF = 2e9;
int n;
int a[maxn], b[maxn];
int L[maxn], R[maxn];
int segment_tree[4 * maxn];
void solve_subtask2() {
    int x = b[0];
    int res = 0;
    for(int i = 0; i < n; i++) {
        if(a[i] == x) {
            res++;
            int j = i - 1;
            while(j >= 0 and a[j] <= x) {
                j--;
                res++;
            }
            j = i + 1;
            while(j < n and a[j] <= x) {
                res++;
                j++;
            }
            i = j - 1;
        }
    }
    cout << res << endl;
}
int dp[5005][5005];
int rec(int i, int j) {
    if(i < 0 or j < 0) {
        return 0;
    }
    if(dp[i][j] != -1) {
        return dp[i][j];
    }
    int res = 0;
    if(a[i] == b[j] and L[i] < j and R[i] > j) {
        res = max(res, rec(i, j - 1) + 1);
    }
    res = max(res, rec(i - 1, j));
    res = max(res, rec(i, j - 1));
    return dp[i][j] = res;
}
void build_tree(int L, int R, int node) {
    if(L == R) {
        segment_tree[node] = 0;
        return;
    }
    int middle = (L + R) / 2;
    build_tree(L, middle, 2 * node);
    build_tree(middle + 1, R, 2 * node + 1);
    segment_tree[node] = max(segment_tree[2 * node], segment_tree[2 * node + 1]);
}
int query(int i, int j, int L, int R, int node) {
    // L  R i L R j L R
    if(i <= L and R <= j) {
        return segment_tree[node];
    }
    if(R < i or j < L) {
        return 0;
    }
    int middle = (L + R) / 2;
    return max(query(i, j, L, middle, 2 * node), query(i, j, middle + 1, R, 2 * node + 1));
}
void update(int idx, int new_value, int L, int R, int node) {
    if(L == R) {
        segment_tree[node] = new_value;
        return;
    }
    int middle = (L + R) / 2;
    if(idx <= middle) {
        update(idx, new_value, L, middle, 2 * node);
    }
    else {
        update(idx, new_value, middle + 1, R, 2 * node + 1);
    }
    segment_tree[node] = max(segment_tree[2 * node], segment_tree[2 * node + 1]);
}
int main() {
    ios_base::sync_with_stdio(false);
    cin >> n;
    set<int> st;
    vector<int> t(n + 5);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
        t[i + 1] = a[i];
    }
    for(int i = 0; i < n; i++) {
        cin >> b[i];
        st.insert(b[i]);
    }
    t[0] = INF;
    t[n + 1] = INF;
    stack<int> s;
    s.push(0);
    for(int i = 1; i <= n; i++) {
        while(t[s.top()] <= t[i]) {
            s.pop();
        }
        L[i - 1] = s.top() - 1;
        s.push(i);
    }
    while(!s.empty()) {
        s.pop();
    }
    s.push(n + 1);
    for(int i = n; i > 0; i--) {
        while(t[s.top()] <= t[i]) {
            s.pop();
        }
        R[i - 1] = s.top() - 1;
        s.push(i);
    }
    
    if((int) st.size() == 1) {
        solve_subtask2();
        return 0;
    }
    else if(n <= 5005){
        memset(dp, -1, sizeof dp);
        cout << rec(n - 1, n - 1) << endl;
    }
    else {
        build_tree(0, n - 1, 1);
        map<int, int> m;
        for(int i = 0; i < n; i++) {
            m[a[i]] = i;
        }
        
        for(int i = 0; i < n; i++) {
            map<int, int>::iterator it = m.find(b[i]);
            if(it != m.end()) {
                int idx = it->second;
                if(L[idx] < i and i < R[idx]) {
                    int dp = query(0, idx, 0, n - 1, 1);
                    update(idx, dp + 1, 0, n - 1, 1);
                }
            }
        }
        cout << query(0, n - 1, 0, n - 1, 1) << endl;
    }
    
    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 98248 KB Output is correct
2 Correct 34 ms 98372 KB Output is correct
3 Correct 34 ms 98308 KB Output is correct
4 Correct 35 ms 98376 KB Output is correct
5 Correct 35 ms 98304 KB Output is correct
6 Correct 36 ms 98280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 4 ms 1036 KB Output is correct
3 Correct 13 ms 3540 KB Output is correct
4 Correct 10 ms 2652 KB Output is correct
5 Correct 21 ms 3392 KB Output is correct
6 Correct 12 ms 2592 KB Output is correct
7 Correct 18 ms 2764 KB Output is correct
8 Correct 20 ms 3356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 98296 KB Output is correct
2 Correct 35 ms 98416 KB Output is correct
3 Correct 55 ms 98824 KB Output is correct
4 Correct 147 ms 99400 KB Output is correct
5 Correct 162 ms 99472 KB Output is correct
6 Correct 163 ms 99420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 256 ms 99396 KB Output is correct
2 Correct 28 ms 5472 KB Output is correct
3 Correct 77 ms 12124 KB Output is correct
4 Correct 80 ms 12888 KB Output is correct
5 Correct 90 ms 12236 KB Output is correct
6 Correct 90 ms 11468 KB Output is correct
7 Correct 84 ms 12484 KB Output is correct
8 Correct 74 ms 11420 KB Output is correct
9 Correct 92 ms 12340 KB Output is correct
10 Correct 64 ms 12064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 98248 KB Output is correct
2 Correct 34 ms 98372 KB Output is correct
3 Correct 34 ms 98308 KB Output is correct
4 Correct 35 ms 98376 KB Output is correct
5 Correct 35 ms 98304 KB Output is correct
6 Correct 36 ms 98280 KB Output is correct
7 Correct 33 ms 98304 KB Output is correct
8 Correct 32 ms 98252 KB Output is correct
9 Correct 33 ms 98380 KB Output is correct
10 Correct 35 ms 98404 KB Output is correct
11 Correct 35 ms 98380 KB Output is correct
12 Correct 35 ms 98376 KB Output is correct
13 Correct 35 ms 98412 KB Output is correct
14 Correct 35 ms 98308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 98248 KB Output is correct
2 Correct 34 ms 98372 KB Output is correct
3 Correct 34 ms 98308 KB Output is correct
4 Correct 35 ms 98376 KB Output is correct
5 Correct 35 ms 98304 KB Output is correct
6 Correct 36 ms 98280 KB Output is correct
7 Correct 38 ms 98296 KB Output is correct
8 Correct 35 ms 98416 KB Output is correct
9 Correct 55 ms 98824 KB Output is correct
10 Correct 147 ms 99400 KB Output is correct
11 Correct 162 ms 99472 KB Output is correct
12 Correct 163 ms 99420 KB Output is correct
13 Correct 33 ms 98304 KB Output is correct
14 Correct 32 ms 98252 KB Output is correct
15 Correct 33 ms 98380 KB Output is correct
16 Correct 35 ms 98404 KB Output is correct
17 Correct 35 ms 98380 KB Output is correct
18 Correct 35 ms 98376 KB Output is correct
19 Correct 35 ms 98412 KB Output is correct
20 Correct 35 ms 98308 KB Output is correct
21 Correct 34 ms 98304 KB Output is correct
22 Correct 40 ms 98508 KB Output is correct
23 Correct 1 ms 456 KB Output is correct
24 Correct 230 ms 99336 KB Output is correct
25 Correct 197 ms 99380 KB Output is correct
26 Correct 173 ms 99256 KB Output is correct
27 Correct 211 ms 99260 KB Output is correct
28 Correct 159 ms 99500 KB Output is correct
29 Correct 184 ms 99428 KB Output is correct
30 Correct 159 ms 99360 KB Output is correct
31 Correct 163 ms 99424 KB Output is correct
32 Correct 160 ms 99380 KB Output is correct