#include <iostream>
#include <vector>
using namespace std;
const int stala=1e5+10;
const int stala2=5010;
int tab[stala];
int tab2[stala];
int przedzialy[stala][2];
int dp[stala2][stala2];
int pref[stala2][stala2];
vector<int>wektor;
vector<int>wektor2;
void wyznacz_przedzial(int ile)
{
for(int i=1;i<=ile;i++)
{
while(!wektor.empty()&&wektor.back()<=tab[i])
{
przedzialy[wektor2.back()][1]=i-1;
wektor.pop_back();
wektor2.pop_back();
}
wektor.push_back(tab[i]);
wektor2.push_back(i);
}
while(!wektor.empty())
{
przedzialy[wektor2.back()][1]=ile;
wektor.pop_back();
wektor2.pop_back();
}
for(int i=ile;i>=1;i--)
{
while(!wektor.empty()&&wektor.back()<=tab[i])
{
przedzialy[wektor2.back()][0]=i+1;
wektor.pop_back();
wektor2.pop_back();
}
wektor.push_back(tab[i]);
wektor2.push_back(i);
}
while(!wektor.empty())
{
przedzialy[wektor2.back()][0]=1;
wektor.pop_back();
wektor2.pop_back();
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int ile;
cin>>ile;
for(int i=1;i<=ile;i++)
{
cin>>tab[i];
}
for(int i=1;i<=ile;i++)
{
cin>>tab2[i];
}
if(ile<=5000)
{
wyznacz_przedzial(ile);
for(int i=1;i<=ile;i++)
{
for(int j=1;j<=ile;j++)
{
if(przedzialy[j][0]<=i&&i<=przedzialy[j][1])
{
dp[i][j]=max(dp[i-1][j],pref[i-1][j-1]);
if(tab[j]==tab2[i])
{
dp[i][j]++;
}
}
pref[i][j]=max(pref[i][j-1],dp[i][j]);
}
}
int res=0;
for(int i=1;i<=ile;i++)
{
res=max(res,dp[ile][i]);
}
cout<<res;
}
else
{
cout<<"0";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
12896 KB |
Output is correct |
2 |
Incorrect |
6 ms |
788 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
5836 KB |
Output is correct |
3 |
Correct |
35 ms |
39960 KB |
Output is correct |
4 |
Correct |
156 ms |
157704 KB |
Output is correct |
5 |
Correct |
176 ms |
165472 KB |
Output is correct |
6 |
Correct |
168 ms |
165616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
2 ms |
2124 KB |
Output is correct |
11 |
Correct |
2 ms |
2124 KB |
Output is correct |
12 |
Correct |
2 ms |
2120 KB |
Output is correct |
13 |
Correct |
2 ms |
2124 KB |
Output is correct |
14 |
Correct |
2 ms |
2124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
4 ms |
5836 KB |
Output is correct |
9 |
Correct |
35 ms |
39960 KB |
Output is correct |
10 |
Correct |
156 ms |
157704 KB |
Output is correct |
11 |
Correct |
176 ms |
165472 KB |
Output is correct |
12 |
Correct |
168 ms |
165616 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
2 ms |
2124 KB |
Output is correct |
17 |
Correct |
2 ms |
2124 KB |
Output is correct |
18 |
Correct |
2 ms |
2120 KB |
Output is correct |
19 |
Correct |
2 ms |
2124 KB |
Output is correct |
20 |
Correct |
2 ms |
2124 KB |
Output is correct |
21 |
Correct |
2 ms |
2124 KB |
Output is correct |
22 |
Correct |
12 ms |
14668 KB |
Output is correct |
23 |
Correct |
157 ms |
118616 KB |
Output is correct |
24 |
Correct |
157 ms |
118804 KB |
Output is correct |
25 |
Correct |
157 ms |
119504 KB |
Output is correct |
26 |
Correct |
164 ms |
126020 KB |
Output is correct |
27 |
Correct |
168 ms |
136736 KB |
Output is correct |
28 |
Correct |
170 ms |
153668 KB |
Output is correct |
29 |
Correct |
178 ms |
165436 KB |
Output is correct |
30 |
Correct |
183 ms |
165456 KB |
Output is correct |
31 |
Correct |
172 ms |
165536 KB |
Output is correct |
32 |
Correct |
168 ms |
143044 KB |
Output is correct |