//n <= 5000, subtask 1, 3, 5, 6
#include<bits/stdc++.h>
using namespace std;
int dp[5005][5005], pre[5005][5005], a[5005], b[5005];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n; cin>>n;
for(int i = 1; i <= n; i++) cin>>a[i];
for(int i = 1; i <= n; i++) cin>>b[i];
int curmaxa = 0;
for(int i = 1; i <= n; i++){
curmaxa = max(curmaxa, a[i]);
if(a[i] == curmaxa) dp[0][i] = 0;
else dp[0][i] = -1e9;
}
for(int i = 1; i <= n; i++){
for(int j = i; j <= n; j++) pre[i][j] = max(pre[i][j-1], a[j]);
}
for(int i = 1; i <= n; i++){
int curbest = 0;
for(int j = 1; j <= n; j++){
curbest = max(curbest, dp[i-1][j]);
dp[i][j] = curbest;
if(pre[min(i, j)][max(i, j)] > a[j]) dp[i][j] = -1e9;
}
//Update b
for(int j = 1; j <= n; j++) if(b[i] == a[j]) dp[i][j]++;
//for(int j = 1; j <= n; j++) cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
}
int ans = 0, curmax = 0;
for(int i = n; i >= 1; i--){
curmax = max(curmax, a[i]);
if(a[i] == curmax) ans = max(ans, dp[n][i]);
}
cout<<ans;
}
# | 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... |