This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> b(n);
for (int i = 0; i < n; i++) cin >> b[i];
vector<int> right(n, n);
vector<int> st;
for (int i = 0; i < n; i++) {
while (!st.empty() && a[st.back()] < a[i]) {
right[st.back()] = i;
st.pop_back();
}
st.push_back(i);
}
vector<int> left(n, -1);
st = {};
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && a[st.back()] < a[i]) {
left[st.back()] = i;
st.pop_back();
}
st.push_back(i);
}
vector<pair<pair<int, int>, int>> events;
for (int i = 0; i < n; i++) {
for (int l = left[i] + 1; l < right[i]; l++) {
int cnt = 0;
for (int r = l; r < right[i]; r++) {
if (b[r] == a[i]) cnt++;
events.push_back({{l, r}, cnt});
}
}
}
vector<int> dp(n + 1, 0);
for (auto e : events) {
int l = e.first.first, r = e.first.second, cnt = e.second;
dp[r + 1] = max(dp[r + 1], dp[l] + cnt);
}
cout << dp[n] << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |