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>
#define f first
#define s second
using namespace std;
const int N=5000+5,M=1e5+5,mod=1e9+7;
int t,b[M],a[M],dp[N],ans,n,tree[4*M],r[M],l[M];
map<int,vector<int> > c;
bool ok(int x,int y) {
if(x<=y && r[x] > y) return 1;
if(x>y && l[x]<y) return 1;
return 0;
}
void update(int u,int ind,int l,int r,int val) {
if(l>ind || r<ind) return;
if(l==r) {
tree[u] = val;
return;
}
int mid=(l+r)/2;
update(2*u,ind,l,mid,val);
update(2*u+1,ind,mid+1,r,val);
tree[u] = max(tree[2*u],tree[2*u+1]);
}
int getans(int u,int start,int end,int l,int r){
if(l>end || r<start) return 0;
if(start<=l && r<=end) return tree[u];
int mid = (l+r)/2;
return max(getans(2*u,start,end,l,mid),getans(2*u+1,start,end,mid+1,r));
}
main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n;
int F = 0;
for(int i=1;i<=n;i++) {
cin >> a[i];
}
int f = 0;
for(int i=1;i<=n;i++){
cin >> b[i];
if(i-1 && b[i]!=b[i-1]) f = 1;
}
for(int i=1;i<=n;i++) {
int x = i - 1;
while(x && a[x] <= a[i]) {
x = l[x];
}
l[i] = x;
}
for(int i=n;i>=1;i--){
int x = i + 1;
while(x<=n && a[x]<=a[i]) {
x = r[x];
}
r[i] = x;
}
int ans = 0;
//f = 1;
if(!f) {
for(int i=1;i<=n;i++){
int j = i - 1,F = 0;
while(j<n && a[j+1] <= b[1]) {
j ++;
if(b[1]==a[j]) F = 1;
}
if(F) ans += j - i + 1;
i = max(i,j);
}
cout<<ans;
return 0;
}
for(int i=1;i<=n;i++){
if(c[a[i]].size()) F = 1;
c[a[i]].push_back(i);
}
if(!F) {
for(int i=1;i<=n;i++) {
if(c[b[i]].size() && ok(c[b[i]][0],i)) {
int x = getans(1,1,c[b[i]][0],1,n);
update(1,c[b[i]][0],1,n,x + 1);
}
}
cout<<tree[1];
return 0;
}
for(int i=1;i<=n;i++) {
for(int j=c[b[i]].size() - 1;j>=0;j--) {
int ind = c[b[i]][j];
dp[ind] = dp[ind] + ok(ind,i);
}
for(int j=1;j<=n;j++) {
dp[j] = max(dp[j],dp[j-1]);
ans = max(dp[j],ans);
} //cout<<endl;
}
cout<<ans;
}
Compilation message (stderr)
exam.cpp:30:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
30 | main(){
| ^~~~
# | 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... |