#include <bits/stdc++.h>
using namespace std;
template <class X, class Y>
bool maximize(X &x, Y y) {
if (x < y) return x = y, true;
else return false;
}
const int inf = (int) 1e9 + 7;
int n;
#define MAX_N 100'100
int a[MAX_N + 2], b[MAX_N + 2];
#undef MAX_N
namespace subtask2 {
bool check() {
return (*max_element(b + 1, b + 1 + n) == *min_element(b + 1, b + 1 + n));
}
void solve() {
int ans = 0, cnt = 0;
bool ok = false;
a[n + 1] = inf;
for (int i = 1; i <= n + 1; ++i) {
if (a[i] > b[1]) {
if (ok) ans += cnt;
cnt = 0;
ok = false;
} else {
++cnt;
if (a[i] == b[1]) ok = true;
}
}
cout << ans;
}
};
namespace subtask3 {
bool check() {
for (int i = 1; i < n; ++i) {
if (a[i] >= a[i + 1]) return false;
}
return (n <= 5000);
}
int dp[5050];
int cost[5050][5050];
void solve() {
assert(false);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cost[j][i] = cost[j - 1][i] + (a[i] == b[j]);
}
}
for (int i = 1; i <= n; ++i) {
dp[i] = dp[i - 1];
for (int j = i; j >= 1; --j) {
maximize(dp[i], dp[j - 1] + cost[i][i] - cost[j - 1][i]);
}
}
cout << dp[n];
}
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(nullptr);
// freopen("test.inp","r",stdin);
// freopen("test.out","w",stdout);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) cin >> b[i];
if (subtask2 :: check()) return subtask2 :: solve(), 0;
if (subtask3 :: check()) return subtask3 :: solve(), 0;
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... |