#include<bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define pb push_back
#define int long long
using namespace std ;
const int maxn = 5002 , maxq = 1e5+10 , mod = 998244353 ,inf = 1e18 ;
int dp[3][maxn][maxn] , a[maxn] , b[maxn] , l[maxn] , r[maxn] ;
stack<int> s , t;
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n ;
cin >>n ;
for(int i =1 ; i <= n ; i++){
cin >> a[i];
}
for(int i= 1; i <= n ; i++){
while(s.size() && a[i] >= a[s.top()])s.pop();
if(s.size() == 0){
l[i] = 0 ;
}else{
l[i] = s.top();
}
s.push(i);
}
for(int i = n; i >= 1 ; i--){
while(t.size() && a[i] >= a[t.top()])t.pop();
if(t.size() == 0){
r[i] = n+1 ;
}else{
r[i] = t.top();
}
t.push(i);
}
for(int i = 1; i <= n ; i++){
cin >> b[i] ;
// cout << l[i] << " " << r[i] <<"<-\n";
}
for(int i = 1; i <= n ; i++){
for(int j =1 ; j <= n ; j++){
dp[0][i][j] = max(dp[0][i-1][j] , dp[1][i-1][j]);
dp[1][i][j] = max(max(dp[0][i-1][j-1] , dp[1][i-1][j-1]) , dp[1][i][j-1]) + ((j > l[i] && r[i] > j) && a[i] == b[j]) ;
}
}
for(int i = 1; i <= n ; i++){
for(int j =1 ; j <= n ; j++){
// cout << dp[0][i][j] << " " << dp[1][i][j] << " ";
}
// cout << "\n";
}
cout << max(dp[0][n][n] , dp[1][n][n]) << "\n";
return 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
24096 KB |
Output is correct |
2 |
Runtime error |
731 ms |
524288 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
8276 KB |
Output is correct |
3 |
Correct |
47 ms |
79224 KB |
Output is correct |
4 |
Correct |
204 ms |
376392 KB |
Output is correct |
5 |
Correct |
215 ms |
392056 KB |
Output is correct |
6 |
Correct |
227 ms |
392096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
231 ms |
392024 KB |
Output is correct |
2 |
Execution timed out |
1106 ms |
477008 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
10 |
Correct |
2 ms |
2516 KB |
Output is correct |
11 |
Correct |
2 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2508 KB |
Output is correct |
13 |
Correct |
1 ms |
2516 KB |
Output is correct |
14 |
Correct |
2 ms |
2516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
4 ms |
8276 KB |
Output is correct |
9 |
Correct |
47 ms |
79224 KB |
Output is correct |
10 |
Correct |
204 ms |
376392 KB |
Output is correct |
11 |
Correct |
215 ms |
392056 KB |
Output is correct |
12 |
Correct |
227 ms |
392096 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
1236 KB |
Output is correct |
16 |
Correct |
2 ms |
2516 KB |
Output is correct |
17 |
Correct |
2 ms |
2516 KB |
Output is correct |
18 |
Correct |
2 ms |
2508 KB |
Output is correct |
19 |
Correct |
1 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2516 KB |
Output is correct |
21 |
Correct |
2 ms |
2516 KB |
Output is correct |
22 |
Correct |
14 ms |
24148 KB |
Output is correct |
23 |
Correct |
232 ms |
392012 KB |
Output is correct |
24 |
Correct |
227 ms |
392064 KB |
Output is correct |
25 |
Correct |
226 ms |
392280 KB |
Output is correct |
26 |
Correct |
220 ms |
391956 KB |
Output is correct |
27 |
Correct |
223 ms |
391980 KB |
Output is correct |
28 |
Correct |
221 ms |
392112 KB |
Output is correct |
29 |
Correct |
218 ms |
392088 KB |
Output is correct |
30 |
Correct |
217 ms |
392120 KB |
Output is correct |
31 |
Correct |
224 ms |
392036 KB |
Output is correct |
32 |
Correct |
214 ms |
392116 KB |
Output is correct |