不知道是不是数据的问题,但是我看到好多人都不是45,求调。
跟正解差得比较远,dp[i][j]表示a取i个b取j个最大长度,mmr[i][j]表示a取i个b取j个在长度最大时末尾数最小值,lmr[i][j]表示a取i个b取j个长度最大、末尾数最小时倒数第二个数的最小值
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long n;
cin >> n;
long long a[n + 5], b[n + 5];
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> b[i];
long long dp[n + 5][n + 5];
long long mmr[n + 5][n + 5], lmr[n + 5][n + 5];
memset(dp, 0, sizeof(dp));
memset(mmr, 0, sizeof(mmr));
memset(lmr, 0, sizeof(lmr));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
if (a[i] > mmr[i - 1][j - 1] && a[i] == b[j])
dp[i][j] = dp[i - 1][j - 1] + 1,
lmr[i][j] = mmr[i - 1][j - 1],
mmr[i][j] = a[i];
else if (a[i] < mmr[i - 1][j - 1] && a[i] > lmr[i - 1][j - 1] && a[i] == b[j])
dp[i][j] = dp[i - 1][j - 1],
lmr[i][j] = lmr[i - 1][j - 1],
mmr[i][j] = a[i];
else
if (dp[i - 1][j] == dp[i][j - 1])
if (mmr[i - 1][j] == mmr[i][j - 1])
dp[i][j] = dp[i - 1][j],
mmr[i][j] = mmr[i - 1][j],
lmr[i][j] = min(lmr[i - 1][j], lmr[i][j - 1]);
else if (mmr[i - 1][j] < mmr[i][j - 1])
dp[i][j] = dp[i - 1][j],
mmr[i][j] = mmr[i - 1][j],
lmr[i][j] = lmr[i - 1][j];
else
dp[i][j] = dp[i][j - 1],
mmr[i][j] = mmr[i][j - 1],
lmr[i][j] = lmr[i][j - 1];
else if (dp[i - 1][j] > dp[i][j - 1])
dp[i][j] = dp[i - 1][j],
mmr[i][j] = mmr[i - 1][j],
lmr[i][j] = lmr[i - 1][j];
else
dp[i][j] = dp[i][j - 1],
mmr[i][j] = mmr[i][j - 1],
lmr[i][j] = lmr[i][j - 1];
// cout << i << " " << j << endl;
// cout << dp[i][j] << " " << mmr[i][j] << " " << lmr[i][j] << endl;
}
cout << dp[n][n] << endl;
return 0;
}