#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <set>
#define x first
#define y second
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
#define all(v) v.begin(), v.end()
using namespace std;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef long long ll;
const int MAX_N = 1e5;
const int mod = 1e9 + 7;
const int MAX = 1e9;
int a[MAX_N], b[MAX_N];
struct Seg{
int n;
vi seg;
Seg(int m) {
for (n = 1; n < m; n <<= 1);
seg.resize(2 * n);
}
void update(int i, int x) {
seg[i + n] = x;
i += n;
for (i >>= 1; i; i >>= 1) seg[i] = max(seg[2 * i], seg[2 * i + 1]);
}
int query(int l, int r) {
int ans = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) chkmax(ans, seg[l++]);
if (r & 1) chkmax(ans, seg[--r]);
}
return ans;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
vi dp(n);
dp[0] = a[0] == b[0];
for (int i = 1; i < n; i++) {
dp[i] = dp[i - 1] + (a[i] == b[i]);
vi prefix(i + 1);
vi suffix(i + 2);
suffix[i] = a[i] == b[i];
for (int j = i - 1; j >= 0; j--) {
prefix[i] = 0;
prefix[j] = dp[j] + suffix[j + 1];
suffix[j] = suffix[j + 1] + (b[j] == a[i]);
}
int ans = 0;
for (int j = 0; j <= i; j++) {
chkmax(ans, prefix[j]);
chkmax(dp[j], ans - suffix[j + 1]);
}
int prefix_sum = 0;
for (int j = 0; j <= i; j++) {
prefix_sum += b[j] == a[i];
chkmax(dp[j], prefix_sum);
}
}
cout << dp[n - 1];
}
//5
//1 2 3 4 6
//4 4 2 6 6
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
14 ms |
332 KB |
Output is correct |
4 |
Correct |
80 ms |
704 KB |
Output is correct |
5 |
Correct |
84 ms |
464 KB |
Output is correct |
6 |
Correct |
84 ms |
568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
85 ms |
464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |