From 7b3f37a5406dde3944ab5de33091ee9b7555eab7 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 20 Nov 2022 11:16:23 +0800 Subject: [PATCH] =?UTF-8?q?P1439=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E6=9C=80=E9=95=BF=E5=85=AC=E5=85=B1=E5=AD=90=E5=BA=8F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/94751747 --- Luogu/P1439/P1439.cpp | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/Luogu/P1439/P1439.cpp b/Luogu/P1439/P1439.cpp index 0a4358b0..92e9a050 100644 --- a/Luogu/P1439/P1439.cpp +++ b/Luogu/P1439/P1439.cpp @@ -1,29 +1,42 @@ #include +#include using std::cin; using std::cout; -using std::endl; +const char endl = '\n'; -const int N = 10005; +const int N = 1e5 + 5; -int n, a[N], b[N], f[N][N]; +int n, cnt, a[N], b[N], c[N], p[N], f[N]; int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + cin >> n; + for (int i = 1; i <= n; i++) { cin >> a[i]; + + p[a[i]] = i; } + for (int i = 1; i <= n; i++) { cin >> b[i]; } + for (int i = 1; i <= n; i++) { - for (int j = 1; j <= n; j++) { - f[i][j] = std::max(f[i - 1][j], f[i][j - 1]); - if (a[i] == b[j]) { - f[i][j] = std::max(f[i][j], f[i - 1][j - 1] + 1); - } + if (p[b[i]] > c[cnt]) { + c[++cnt] = p[b[i]]; + f[i] = cnt; + } else { + int k = std::lower_bound(c + 1, c + cnt + 1, p[b[i]]) - c; + c[k] = p[b[i]]; + f[i] = k; } } - cout << f[n][n] << endl; + + cout << cnt << endl; + return 0; }