This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const long long int N = 5011;
long long int a[N],b[N];
long long int dp[N][N];
long long int mx[N][N];
vector<long long int> vv;
long long int c;
map<long long int,long long int> mt;
long long int amount[N*5];
int main()
{
long long 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]);
}
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]<<"\n";
}
Compilation message (stderr)
exam.cpp: In function 'int main()':
exam.cpp:26:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for (int i=0; i<vv.size(); i++)
| ~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |