#include<bits/stdc++.h>
using namespace std;
int n;
int a[5005], b[5005];
int sus[5005][5005];
int f[5005];
int cnt[5005];
vector<vector<int>> ps(5005);
vector<vector<int>> fuq(5005);
int pre[5005];
int suf[5005];
bool check[5005];
namespace sub1{
void solve(){
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j ++) check[j] = 0;
for (int j = 1; j <= n; j ++){
for (auto k : ps[j]){
if (k >= i){
pre[k] = k;
suf[k] = k;
check[k] = 1;
if (check[k - 1]){
pre[k] = pre[k - 1];
suf[pre[k]] = k;
}
if (check[k + 1]){
pre[suf[k + 1]] = pre[k];
suf[pre[k]] = suf[k + 1];
}
}
}
int hic = 0;
while (hic < fuq[j].size() && fuq[j][hic] < i){
hic ++;
}
int sl = 0;
// cout << suf[i] << " " << j << " " << i << endl;
for (auto k : ps[j]){
if (k > suf[i]) break;
if (k >= i){
if (hic < fuq[j].size()){
if (fuq[j][hic] > suf[i]) break;
sl ++;
sus[i][fuq[j][hic]] = max(sus[i][fuq[j][hic]], sl);
hic ++;
}
}
}
}
for (int j = i + 1; j <= n; j ++){
sus[i][j] = max(sus[i][j], sus[i][j - 1]);
}
}
for (int i = 1; i <= n; i ++){
for (int j = 1; j <= i; j ++){
f[i] = max(f[i], f[j - 1] + sus[j][i]);
}
}
cout << f[n] << endl;
}
};
namespace sub2{
};
namespace sub4{
};
int main(){
cin >> n;
map<int, int> hihi;
for (int i = 1; i <= n; i ++){
cin >> a[i];
hihi[a[i]] = 1;
}
for (int i = 1; i <= n; i ++){
cin >> b[i];
hihi[b[i]] = 1;
}
int cnt = 1;
for (auto i : hihi){
hihi[i.first] = cnt;
cnt ++;
}
for (int i = 1; i <= n; i ++ ){
a[i] = hihi[a[i]];
b[i] = hihi[b[i]];
ps[a[i]].push_back(i);
fuq[b[i]].push_back(i);
}
sub1::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... |