#include <bits/stdc++.h>
#define INF 1e18
#define LL long long
#define int long long
using namespace std;
const int mod = 1e9 + 7;
const int inf = 1e18;
const int mxn = 1e5 +5;
int n;
int A[mxn];
int B[mxn];
int l[mxn];
int r[mxn];
void two_pointers(){
int x = B[0];
int res = 0;
for(int i = 0;i<n;i++) {
if(A[i] == x) {
res++;
int j = i - 1;
while(j >= 0 && A[j] <= x) {
j--;
res++;
}
j = i + 1;
while(j < n && A[j] <= x) {
res++;
j++;
}
i = j - 1;
}
}
cout<<res<<endl;
}
int dp[5005][5005];
bool vis[5005][5005];
int f(int i,int j){
if(i == -1 ||j == -1)return 0;
if(vis[i][j])return dp[i][j];
int ans = 0;
if(A[i] == B[j] && l[i] < j && r[i] > j){
ans = max(ans, f(i,j-1) + 1);
}
ans = max(ans , f(i-1,j));
ans = max(ans, f(i,j-1));
vis[i][j] = true;
dp[i][j] = ans;
return ans;
}
void solve(){
cin>>n;
set<int>s;
int c[n+5];
for(int i = 0;i<n;i++)cin>>A[i];
for(int i = 0;i<n;i++){
cin>>B[i];
s.insert(B[i]);
}
for(int i = 0;i<n;i++)c[i+1] = A[i];
c[0] = inf;
c[n+1] = inf;
stack<int> st;
st.push(0);
for(int i = 1;i<=n;i++){
while(st.size() && (c[st.top()] <= c[i]))st.pop();
l[i-1] = st.top() - 1;
st.push(i);
}
//st = stack<int>();
while(st.size())st.pop();
st.push(n+1);
for(int i = n;i;i--){
while(st.size() && (c[st.top()] <= c[i]))st.pop();
//cout<<"tukaa"<<endl;
r[i-1] = st.top() - 1;
//if(r[i-1] >= n)r[i-1] = -1;
st.push(i);
}
bool sub2 = true;
for(int i = 1;i<n;i++){
if(B[i] != B[0]){
sub2 = false;
break;
}
}
if(sub2){
two_pointers();
return;
}
if(n <= 5000){
cout<<f(n-1,n-1)<<endl;
return;
}
}
int32_t main() {
#ifdef ONLINE_JUDGE
ios_base::sync_with_stdio(false);
cin.tie(0);
#endif
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
15 ms |
1492 KB |
Output is correct |
3 |
Correct |
37 ms |
5588 KB |
Output is correct |
4 |
Correct |
24 ms |
4496 KB |
Output is correct |
5 |
Correct |
61 ms |
6240 KB |
Output is correct |
6 |
Correct |
25 ms |
4496 KB |
Output is correct |
7 |
Correct |
28 ms |
4640 KB |
Output is correct |
8 |
Correct |
62 ms |
6112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
5 ms |
6612 KB |
Output is correct |
3 |
Correct |
49 ms |
49964 KB |
Output is correct |
4 |
Correct |
208 ms |
212984 KB |
Output is correct |
5 |
Correct |
214 ms |
221808 KB |
Output is correct |
6 |
Correct |
239 ms |
221900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
704 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
564 KB |
Output is correct |
9 |
Correct |
1 ms |
1208 KB |
Output is correct |
10 |
Correct |
2 ms |
2260 KB |
Output is correct |
11 |
Correct |
1 ms |
2260 KB |
Output is correct |
12 |
Correct |
1 ms |
2260 KB |
Output is correct |
13 |
Correct |
2 ms |
2260 KB |
Output is correct |
14 |
Correct |
1 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
5 ms |
6612 KB |
Output is correct |
9 |
Correct |
49 ms |
49964 KB |
Output is correct |
10 |
Correct |
208 ms |
212984 KB |
Output is correct |
11 |
Correct |
214 ms |
221808 KB |
Output is correct |
12 |
Correct |
239 ms |
221900 KB |
Output is correct |
13 |
Correct |
0 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
564 KB |
Output is correct |
15 |
Correct |
1 ms |
1208 KB |
Output is correct |
16 |
Correct |
2 ms |
2260 KB |
Output is correct |
17 |
Correct |
1 ms |
2260 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
2 ms |
2260 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
2 ms |
2260 KB |
Output is correct |
22 |
Correct |
15 ms |
17340 KB |
Output is correct |
23 |
Correct |
4 ms |
596 KB |
Output is correct |
24 |
Correct |
317 ms |
221684 KB |
Output is correct |
25 |
Correct |
235 ms |
221728 KB |
Output is correct |
26 |
Correct |
319 ms |
221580 KB |
Output is correct |
27 |
Correct |
221 ms |
221644 KB |
Output is correct |
28 |
Correct |
214 ms |
221844 KB |
Output is correct |
29 |
Correct |
266 ms |
221836 KB |
Output is correct |
30 |
Correct |
273 ms |
221788 KB |
Output is correct |
31 |
Correct |
214 ms |
221824 KB |
Output is correct |
32 |
Correct |
229 ms |
221976 KB |
Output is correct |