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;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=1e5+7;
int a[LIM], b[LIM], dp[5007][5007], ma[5007][5007], n;
int tr[4*LIM], tr2[4*LIM], N=1;
map<int,int>mp;
void subtask2() {
rep(i, n-1) if(a[i+1]==b[1] && a[i+1]>=a[i+2]) a[i+2]=a[i+1];
for(int i=n-1; i; --i) if(a[i+1]==b[1] && a[i+1]>=a[i]) a[i]=a[i+1];
int ans=0;
rep(i, n) if(a[i+1]==b[i+1]) ++ans;
cout << ans << '\n';
}
int cnt(int v) {
v+=N;
int ans=tr[v];
while(v) {
if(v%2==1) ans=max(ans, tr[v-1]);
v/=2;
}
return ans;
}
void upd(int v, int x) {
v+=N;
while(v) {
tr[v]=max(tr[v], x);
v/=2;
}
}
int cnt2(int l, int r) {
l+=N; r+=N;
int ans=max(tr2[l], tr2[r]);
while(l/2!=r/2) {
if(l%2==0) ans=max(ans, tr2[l+1]);
if(r%2==1) ans=max(ans, tr2[r-1]);
l/=2; r/=2;
}
return ans;
}
void subtask4() {
while(N<=n) N*=2;
rep(i, n) tr2[N+i+1]=a[i+1];
for(int i=N-1; i; --i) tr2[i]=max(tr2[2*i], tr2[2*i+1]);
rep(i, n) mp[a[i+1]]=i+1;
rep(i, n) {
if(!mp[b[i+1]] || cnt2(min(mp[b[i+1]], i+1), max(mp[b[i+1]], i+1))>b[i+1]) continue;
upd(mp[b[i+1]], cnt(mp[b[i+1]])+1);
}
cout << cnt(n) << '\n';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
rep(i, n) cin >> a[i+1];
rep(i, n) cin >> b[i+1];
bool ok2=true;
rep(i, n-1) if(b[i+1]!=b[i+2]) ok2=false;
if(ok2) {
subtask2();
return 0;
}
if(n>5000) {
subtask4();
return 0;
}
rep(i, n) {
for(int j=i; j<n; ++j) {
ma[i+1][j+1]=max(ma[i+1][j], a[j+1]);
}
}
rep(i, n) rep(j, n) {
dp[i+1][j+1]=max(dp[i+1][j], dp[i][j+1]);
if(a[i+1]==b[j+1] && ma[min(i+1, j+1)][max(i+1, j+1)]==a[i+1]) {
dp[i+1][j+1]=max(dp[i+1][j+1], dp[i+1][j]+1);
}
}
cout << dp[n][n] << '\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... |