Submission #516731

#TimeUsernameProblemLanguageResultExecution timeMemory
516731nickyrioExam (eJOI20_exam)C++17
65 / 100
110 ms99548 KiB
#include <bits/stdc++.h>

using namespace std;
#define all(s) s.begin(), s.end() 
const int N = 5050;
int a[N], b[N], n, dp[N][N], L[N], R[N];
vector<int> e[N];

void sub2() {
    for (int i = 2; i <= n; ++i) if (b[i] != b[1]) return;
    int cnt = 0;
    // cerr << "Sub 2" << '\n';
    for (int i = 1; i <= n; ++i) if (a[i] == b[1]) {
        // cerr << i << ' ' << L[i] << ' ' << R[i] << '\n';
        for (int j = L[i] + 1; j < R[i]; ++j) a[j] = b[1];
    }
    for (int i = 1; i <= n; ++i) if (a[i] == b[1]) ++cnt;
    cout << cnt << '\n';
    exit(0);
}
int main() {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    for (int i = 1; i <= n; ++i) cin >> b[i];
    a[0] = a[n + 1] = 2e9;
    for (int i = 1; i <= n; ++i) {
        L[i] = R[i] = i;
        while (a[L[i]] <= a[i]) --L[i];
        while (a[R[i]] <= a[i]) ++R[i];
    }
    sub2();
    vector<int> Ranking(a + 1, a + n + 1);
    sort(all(Ranking));
    Ranking.resize(unique(all(Ranking)) - Ranking.begin());
    for (int i = 1; i <= n; ++i) {
        a[i] = lower_bound(all(Ranking), a[i]) - Ranking.begin();
        int temp_b = lower_bound(all(Ranking), b[i]) - Ranking.begin();
        if (temp_b != Ranking.size() && Ranking[temp_b] != b[i]) temp_b = Ranking.size();
        b[i] = temp_b;
        e[b[i]].push_back(i);
    }

    for (int i = 1; i <= n; ++i) {
        int temp = 1;
        int p = 0, cnt = 0;
        for (int j : e[a[i]]) if (L[i] < j && j < R[i]) {
            ++cnt;
            if (dp[i - 1][j - 1] + 1 > dp[i - 1][p] + cnt) { p = j - 1; cnt = 1; }
            dp[i][j] = max(dp[i][j], dp[i - 1][p] + cnt);
        }
        for (int j = 1; j <= n; ++j) dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1]));
    }
    cout << dp[n][n] << '\n';
}

Compilation message (stderr)

exam.cpp: In function 'int main()':
exam.cpp:38:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |         if (temp_b != Ranking.size() && Ranking[temp_b] != b[i]) temp_b = Ranking.size();
      |             ~~~~~~~^~~~~~~~~~~~~~~~~
exam.cpp:44:13: warning: unused variable 'temp' [-Wunused-variable]
   44 |         int temp = 1;
      |             ^~~~
#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...