#include<bits/stdc++.h>
#define f first
#define s second
#define int long long
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(){
cin>>n;
int F = 0;
for(int i=1;i<=n;i++) {
cin >> a[i];
if(a[i] < a[i-1]) F = 1;
}
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++){
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)) {
update(1,c[b[i]][0],1,n,getans(1,1,c[b[i]][0],1,n) + 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
exam.cpp:31:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
31 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
19 ms |
840 KB |
Output is correct |
3 |
Correct |
57 ms |
4120 KB |
Output is correct |
4 |
Correct |
41 ms |
3808 KB |
Output is correct |
5 |
Correct |
101 ms |
5380 KB |
Output is correct |
6 |
Correct |
38 ms |
3712 KB |
Output is correct |
7 |
Correct |
44 ms |
3940 KB |
Output is correct |
8 |
Correct |
101 ms |
5264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
4 ms |
588 KB |
Output is correct |
4 |
Correct |
8 ms |
1024 KB |
Output is correct |
5 |
Correct |
9 ms |
1136 KB |
Output is correct |
6 |
Correct |
9 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
106 ms |
1040 KB |
Output is correct |
2 |
Execution timed out |
1081 ms |
5900 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
4 ms |
588 KB |
Output is correct |
10 |
Correct |
8 ms |
1024 KB |
Output is correct |
11 |
Correct |
9 ms |
1136 KB |
Output is correct |
12 |
Correct |
9 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
6 ms |
332 KB |
Output is correct |
23 |
Correct |
5 ms |
460 KB |
Output is correct |
24 |
Correct |
169 ms |
460 KB |
Output is correct |
25 |
Correct |
119 ms |
588 KB |
Output is correct |
26 |
Correct |
105 ms |
588 KB |
Output is correct |
27 |
Correct |
111 ms |
576 KB |
Output is correct |
28 |
Correct |
105 ms |
872 KB |
Output is correct |
29 |
Correct |
7 ms |
972 KB |
Output is correct |
30 |
Correct |
103 ms |
872 KB |
Output is correct |
31 |
Correct |
107 ms |
876 KB |
Output is correct |
32 |
Correct |
102 ms |
844 KB |
Output is correct |