// #pragma optimize ("g",on)
// #pragma GCC optimize ("inline")
// #pragma GCC optimize ("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC optimize ("03")
#include <bits/stdc++.h>
#define pb push_back
#define SS ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
#define int long long
#define all(v) v.begin(),v.end()
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int N = 5000 + 1, inf = 1e9, mod = 998244353;
int n;
int a[N], b[N];
struct tree{
vector<int> t;
tree(){
t.resize(4 * N);
}
void upd(int v, int tl, int tr, int pos, int x){
if(tl == tr){
t[v] = max(t[v], x);
return;
}
int mid = (tl + tr) / 2;
if(mid >= pos) upd(v * 2, tl, mid, pos, x);
else upd(v * 2 + 1, mid + 1, tr, pos, x);
t[v] = max(t[v * 2], t[v * 2 + 1]);
}
int get(int v, int tl, int tr, int l, int r){
if(tl > r || l > tr) return 0ll;
if(tl >= l && r >= tr) return t[v];
int mid = (tl + tr) / 2;
return max(get(v * 2, tl, mid, l, r), get(v * 2 + 1, mid + 1, tr, l, r));
}
};
void solve(){
cin >> n;
tree t[n + 1];
vector<int> v;
for(int i = 1; i <= n; i++){
cin >> a[i];
v.push_back(a[i]);
}
for(int i = 1; i <= n; i++){
cin >> b[i];
v.push_back(b[i]);
}
sort(all(v));
v.erase(unique(all(v)), v.end());
for(int i = 1; i <= n; i++){
a[i] = lower_bound(all(v), a[i]) - v.begin() + 1;
b[i] = lower_bound(all(v), b[i]) - v.begin() + 1;
}
int dp[n + 1][n + 1] = {}, last[n + 1] = {};
for(int i = 1; i <= n; i++){
int mx = a[i];
int cnt[n + 1] = {};
if(a[i] == b[i]){
t[i].upd(1, 1, n, i, t[i - 1].get(1, 1, n, 1, i - 1) + 1);
}
for(int l = i - 1; l >= 1; l--){
cnt[b[l]]++;
mx = max(mx, a[l]);
// if(mx == a[i]){
// t[i].upd(1, 1, n, l, t[i - 1].get(1, 1, n, l, i - 1) + cnt[a[i]]);
// }
if(mx == b[i]){
t[i].upd(1, 1, n, l, t[i - 1].get(1, 1, n, 1, l) + cnt[mx]);
// t[i].upd(1, 1, n, l, t[i - 1].get(1, 1, n, l, i - 1) + );
}
else t[i].upd(1, 1, n, l, t[i - 1].get(1, 1, n, 1, l));
// cout << t[i - 1].get(1, 1, n, 1, l - 1) + cnt[mx] << ' ' << i << ' ' << l << '\n';
}
}
cout << t[n].get(1, 1, n, 1, n);
}
signed main(){
SS
// freopen("trains.in", "r", stdin);
// freopen("trains.out", "w", stdout);
int t = 1;
// cin >> t;
while(t--){
solve();
}
}
| # | 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... |