//n <= 5000, subtask 1, 3, 5, 6
#include<bits/stdc++.h>
using namespace std;
int dp[5005], a[5005], b[5005], f[10005];
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];
vector<int> vec1, vec2;
for(int i = 1; i <= n; i++) vec1.push_back(a[i]);
for(int i = 1; i <= n; i++) vec1.push_back(b[i]);
sort(vec1.begin(), vec1.end());
for(int i = 0; i < vec1.size(); i++) if(i == 0 || vec1[i] != vec1[i-1]) vec2.push_back(vec1[i]);
for(int i = 1; i <= n; i++) a[i] = upper_bound(vec2.begin(), vec2.end(), a[i]) - vec2.begin();
for(int i = 1; i <= n; i++) b[i] = upper_bound(vec2.begin(), vec2.end(), b[i]) - vec2.begin();
dp[0] = 0;
for(int i = 1; i <= n; i++){
dp[i] = dp[i-1];
int curmax = 0;
memset(f, 0, sizeof(f));
for(int j = i; j >= 1; j--){
f[b[j]]++; curmax = max(curmax, a[j]);
dp[i] = max(dp[i], dp[j-1] + f[curmax]);
}
}
cout<<dp[n];
}
# | 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... |