#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 100005, N0 = 203;
int n;
int a[N], b[N];
int ul[N0], ur[N0];
int maxi[N0][N0];
int ql[N0][N0], qr[N0][N0];
int dp[N0][N0];
int dpl[N0][N0];
int dpr[N0][N0];
void solv0()
{
for (int i = 1; i <= n; ++i)
{
if (a[i] > b[i])
continue;
for (int j = i; j >= 1; --j)
{
if (a[j] == b[i])
{
ul[i] = j;
break;
}
if (a[j] > b[i])
{
break;
}
}
for (int j = i; j <= n; ++j)
{
if (a[j] == b[i])
{
ur[i] = j;
break;
}
if (a[j] > b[i])
{
break;
}
}
}
for (int l = 1; l <= n; ++l)
{
maxi[l][l] = l;
for (int r = l + 1; r <= n; ++r)
{
maxi[l][r] = maxi[l][r - 1];
if (a[r] > a[maxi[l][r]])
maxi[l][r] = r;
}
}
for (int i = 1; i <= n; ++i)
{
for (int j = i; j >= 1; --j)
{
qr[j][i] = qr[j + 1][i] + (b[j] == a[i]);
}
for (int j = i; j <= n; ++j)
{
ql[i][j] = ql[i][j - 1] + (b[j] == a[i]);
}
}
for (int d = 1; d <= n; ++d)
{
for (int l = 1; l <= n - d + 1; ++l)
{
int r = l + d - 1;
int m = maxi[l][r];
dpl[l][m] = max(dpl[l][m], dp[l][m - 1] + (a[m] == b[m]));
dpr[m][r] = max(dpr[m][r], dp[m + 1][r] + (a[m] == b[m]));
dpl[l][m] = max(dpl[l][m], qr[l][m]);
dpr[m][r] = max(dpr[m][r], ql[m][r]);
dp[l][r] = dpl[l][m] + dpr[m][r] - (a[m] == b[m]);
if (ul[l - 1])
{
dpr[ul[l - 1]][r] = max(dpr[ul[l - 1]][r], ql[ul[l - 1]][l - 1] + dp[l][r]);
}
if (ur[r + 1])
{
dpr[l][ur[r + 1]] = max(dpr[l][ur[r + 1]], qr[r + 1][ur[r + 1]] + dp[l][r]);
}
}
}
cout << dp[1][n] << "\n";
}
void solv()
{
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
for (int i = 1; i <= n; ++i)
cin >> b[i];
if (n <= 5000)
{
solv0();
return;
}
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif // SOMETHING
ios_base::sync_with_stdio(false), cin.tie(0);
int tt = 1;
//cin >> tt;
while (tt--)
{
solv();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
9 ms |
3504 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |