#include<bits/stdc++.h>
using namespace std;
const int N = 5011;
int a[N],b[N];
int dp[N][N];
int mx[N][N];
vector<int> vv;
int c;
map<int,int> mt;
int amount[N*5];
void subtask1356(vector<int> &v,int n,int a[],int b[])
{
sort(vv.begin(),vv.end());
for (int i=0; i<vv.size(); i++)
{
if(mt[vv[i]]==0)
{
++c;
mt[vv[i]]=c;
}
}
for(int i=1; i<=n; i++)
a[i]=mt[a[i]];
for(int i=1; i<=n; i++)
b[i]=mt[b[i]];
for(int i=0; i<=n; i++)
for(int j=i; j<=n; j++)
mx[i][j]=max(mx[i][j-1],a[j]);
dp[1][1]=0;
if(a[1]==b[1])
dp[1][1]=1;
for(int j=1; j<=n; j++)
{
for(int i=1; i<=c; i++)
amount[i]=0;
for(int i=j; i<=n; i++)
{
if(i==j)
dp[i][j]=max(dp[i][j],dp[i-1][j-1]+(a[i]==b[i]));
++amount[b[i]];
if(i+1<=n)
{
if(a[i+1]<=mx[j][i])
{
if(b[i+1]==mx[j][i])
dp[i+1][j]=max(dp[i+1][j],dp[i][j]+1);
else
dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
}
else
{
dp[i+1][j]=max(dp[i+1][j],
dp[i][j]-amount[mx[j][i]]+amount[mx[j][i+1]]+(b[i+1]==a[i+1]));
}
}
if(j+1<=i)
dp[i][j+1]=max(dp[i][j+1],dp[i][j]);
}
}
cout<<dp[n][n];
cout<<"\n";
}
int main()
{
int t;
t=1;
while(t--)
{
int n;
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>a[i];
vv.push_back(a[i]);
}
for(int i=1; i<=n; i++)
{
cin>>b[i];
vv.push_back(b[i]);
}
subtask1356(vv,n,a,b);
}
}
Compilation message
exam.cpp: In function 'void subtask1356(std::vector<int>&, int, int*, int*)':
exam.cpp:14:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for (int i=0; i<vv.size(); i++)
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12396 KB |
Output is correct |
2 |
Runtime error |
13 ms |
620 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
6 ms |
5356 KB |
Output is correct |
3 |
Correct |
50 ms |
32236 KB |
Output is correct |
4 |
Correct |
286 ms |
126828 KB |
Output is correct |
5 |
Correct |
315 ms |
134764 KB |
Output is correct |
6 |
Correct |
315 ms |
134764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
306 ms |
134764 KB |
Output is correct |
2 |
Runtime error |
13 ms |
568 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |