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;
#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]);*/
for (int i = m; i <= r; ++i)
dpr[m][r] = max(dpr[m][r], ql[m][i] + dp[i + 1][r]);
for (int i = m; i >= l; --i)
dpl[l][m] = max(dpl[l][m], qr[l][m] + dp[l][i - 1]);
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])
{
dpl[l][ur[r + 1]] = max(dpl[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;
}
# | 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... |