#include<bits/stdc++.h>
#define FOR(i, begin, end) for(int i=(begin); i<(end); i++)
using namespace std;
const int N=5e3+10, L=28;
int n, a[N], b[N], st[N][L], dp[N][N];
void build()
{
FOR(i, 1, L)
{
for(int j=1; j+(1<<i)-1<=n; j++)
{
st[j][i]=max(st[j][i-1], st[j+(1<<(i-1))][i-1]);
}
}
}
int get(int l, int r)
{
int ln=r-l+1;
int k=0;
while((1<<(k+1))<=ln)
{
k++;
}
return max(st[l][k], st[r-(1<<k)+1][k]);
}
int main()
{
cin >> n;
FOR(i, 0, n)
{
cin >> a[i+1];
st[i+1][0]=a[i+1];
}
FOR(i, 0, n) cin >> b[i+1];
build();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i]==b[j])
{
int mx=get(min(i, j), max(i, j));
if(mx==a[i])
{
dp[i][j]+=dp[i][j-1]+1;
}
else
{
dp[i][j]=max({ dp[i-1][j], dp[i][j-1], dp[i-1][j-1] });
}
}
else
{
dp[i][j]=max({ dp[i-1][j], dp[i][j-1] , dp[i-1][j-1] });
}
}
}
cout << dp[n][n]<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12124 KB |
Output is correct |
2 |
Runtime error |
5 ms |
4956 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
3 ms |
5476 KB |
Output is correct |
3 |
Correct |
25 ms |
31836 KB |
Output is correct |
4 |
Correct |
106 ms |
97096 KB |
Output is correct |
5 |
Correct |
112 ms |
99100 KB |
Output is correct |
6 |
Correct |
112 ms |
99088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
99168 KB |
Output is correct |
2 |
Runtime error |
6 ms |
4956 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
3420 KB |
Output is correct |
11 |
Correct |
1 ms |
3420 KB |
Output is correct |
12 |
Correct |
1 ms |
3420 KB |
Output is correct |
13 |
Correct |
1 ms |
3420 KB |
Output is correct |
14 |
Correct |
1 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2392 KB |
Output is correct |
8 |
Correct |
3 ms |
5476 KB |
Output is correct |
9 |
Correct |
25 ms |
31836 KB |
Output is correct |
10 |
Correct |
106 ms |
97096 KB |
Output is correct |
11 |
Correct |
112 ms |
99100 KB |
Output is correct |
12 |
Correct |
112 ms |
99088 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
3420 KB |
Output is correct |
17 |
Correct |
1 ms |
3420 KB |
Output is correct |
18 |
Correct |
1 ms |
3420 KB |
Output is correct |
19 |
Correct |
1 ms |
3420 KB |
Output is correct |
20 |
Correct |
1 ms |
3420 KB |
Output is correct |
21 |
Correct |
2 ms |
3420 KB |
Output is correct |
22 |
Correct |
8 ms |
12120 KB |
Output is correct |
23 |
Correct |
164 ms |
99216 KB |
Output is correct |
24 |
Correct |
161 ms |
99148 KB |
Output is correct |
25 |
Correct |
114 ms |
99156 KB |
Output is correct |
26 |
Correct |
111 ms |
99080 KB |
Output is correct |
27 |
Correct |
114 ms |
99132 KB |
Output is correct |
28 |
Correct |
114 ms |
99056 KB |
Output is correct |
29 |
Correct |
111 ms |
99060 KB |
Output is correct |
30 |
Correct |
112 ms |
99152 KB |
Output is correct |
31 |
Correct |
111 ms |
99136 KB |
Output is correct |
32 |
Correct |
113 ms |
99188 KB |
Output is correct |