// __builtin_popcount(x) broj bitova
// __builtin_popcountll(x) long long
#define here cerr<<"---------------------------\n"
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define rall(a) a.begin(),a.end(),greater<int>()
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
void setIO(string inoutname)
{
freopen((inoutname+".in").c_str(),"r",stdin);
freopen((inoutname+".out").c_str(),"w",stdout);
}
ll gcd(ll a, ll b)
{
if(b==0) return a;
if(a==0) return b;
if(a>=b) return gcd(a%b,b);
return gcd(a,b%a);
}
#define maxn 100005
#define maxn2 5005
#define logn 30
ll n;
ll a[maxn];
ll b[maxn];
ll dp[maxn2][maxn2];
ll st[maxn2][logn];
ll get(ll l,ll r){
ll len = r-l+1;
ll k = 0;
while((1<<(k+1))<=len){
k++;
}
return max(st[l][k],st[r-(1<<k)+1][k]);
}
void tc(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
cin >> n;
for(ll i = 1;i<=n;i++) cin >> a[i];
bool s2 = 1;
for(ll i = 1;i<=n;i++){
cin >> b[i];
if(i>1&&b[i]!=b[i-1]) s2 = 0;
}
if(s2){
ll cnt = 0;
bool e = 0;
ll ans = 0;
for(ll i = 1;i<=n;i++){
if(a[i]>b[1]){
if(e) ans+=cnt;
cnt = 0;
e = 0;
}else{
cnt++;
if(a[i]==b[1]) e = 1;
}
}
if(e) ans+=cnt;
cout<<ans<<endl;
return;
}
for(ll i = 1;i<=n;i++) st[i][0] = a[i];
for(ll j = 1;j<logn;j++){
for(ll i = 1;i+(1<<(j-1))<=n;i++){
st[i][j] = max(st[i][j-1],st[i+(1<<(j-1))][j-1]);
}
}
for(ll i = 1;i<=n;i++){
for(ll j = 1;j<=n;j++){
if(a[i]==b[j]){
ll mx = get(min(i,j),max(i,j));
if(mx==a[i]) dp[i][j] = max(dp[i][j-1],dp[i-1][j-1]) + 1;
else{
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
dp[i][j] = max(dp[i][j],dp[i-1][j-1]);
}
}else{
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
dp[i][j] = max(dp[i][j],dp[i-1][j-1]);
}
}
}
cout<<dp[n][n]<<endl;
}
int main(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
//setIO("lol");
int t; t = 1;
while(t--){
tc();
}
return 0;
}
/*
4
10 1 9 2
10 9 10 9
2
out: 2
--
3
10 1 1
1 1 1
*/
Compilation message
exam.cpp: In function 'void setIO(std::string)':
exam.cpp:32:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen((inoutname+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
exam.cpp:33:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((inoutname+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
5 ms |
588 KB |
Output is correct |
3 |
Correct |
19 ms |
1612 KB |
Output is correct |
4 |
Correct |
15 ms |
1832 KB |
Output is correct |
5 |
Correct |
27 ms |
1824 KB |
Output is correct |
6 |
Correct |
15 ms |
1812 KB |
Output is correct |
7 |
Correct |
16 ms |
1792 KB |
Output is correct |
8 |
Correct |
25 ms |
1824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
4428 KB |
Output is correct |
3 |
Correct |
37 ms |
40176 KB |
Output is correct |
4 |
Correct |
185 ms |
189544 KB |
Output is correct |
5 |
Correct |
196 ms |
197388 KB |
Output is correct |
6 |
Correct |
196 ms |
197572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
197480 KB |
Output is correct |
2 |
Execution timed out |
1098 ms |
202556 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
836 KB |
Output is correct |
10 |
Correct |
2 ms |
1484 KB |
Output is correct |
11 |
Correct |
1 ms |
1484 KB |
Output is correct |
12 |
Correct |
2 ms |
1484 KB |
Output is correct |
13 |
Correct |
1 ms |
1484 KB |
Output is correct |
14 |
Correct |
1 ms |
1484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
4 ms |
4428 KB |
Output is correct |
9 |
Correct |
37 ms |
40176 KB |
Output is correct |
10 |
Correct |
185 ms |
189544 KB |
Output is correct |
11 |
Correct |
196 ms |
197388 KB |
Output is correct |
12 |
Correct |
196 ms |
197572 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
836 KB |
Output is correct |
16 |
Correct |
2 ms |
1484 KB |
Output is correct |
17 |
Correct |
1 ms |
1484 KB |
Output is correct |
18 |
Correct |
2 ms |
1484 KB |
Output is correct |
19 |
Correct |
1 ms |
1484 KB |
Output is correct |
20 |
Correct |
1 ms |
1484 KB |
Output is correct |
21 |
Correct |
1 ms |
1484 KB |
Output is correct |
22 |
Correct |
11 ms |
12492 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
316 ms |
197460 KB |
Output is correct |
25 |
Correct |
216 ms |
197676 KB |
Output is correct |
26 |
Correct |
213 ms |
197604 KB |
Output is correct |
27 |
Correct |
213 ms |
197444 KB |
Output is correct |
28 |
Correct |
204 ms |
197692 KB |
Output is correct |
29 |
Correct |
197 ms |
197496 KB |
Output is correct |
30 |
Correct |
196 ms |
197488 KB |
Output is correct |
31 |
Correct |
195 ms |
197448 KB |
Output is correct |
32 |
Correct |
200 ms |
197472 KB |
Output is correct |