比 P1439 排列LCS问题,难那么一点点,只不过有的元素不是两个串都有,还有数据范围变大,树状数组得打离散化。
不过如果用栈+二分的话还是一样的。
——代码
1 #include <cstdio> 2 #include <algorithm> 3 4 const int MAXN = 300001; 5 int n, m, size, ans; 6 int a[MAXN], b[MAXN], c[MAXN << 1], q[MAXN << 1], p[MAXN << 1]; 7 8 inline int max(int x, int y) 9 { 10 return x > y ? x : y; 11 } 12 13 inline int query(int x) 14 { 15 int sum = 0; 16 for(; x; x -= x & -x) sum = max(sum, c[x]); 17 return sum; 18 } 19 20 inline void update(int x, int d) 21 { 22 for(; x <= size; x += x & -x) c[x] = max(c[x], d); 23 } 24 25 int main() 26 { 27 int i, j, x; 28 scanf("%d %d", &n, &m); 29 for(i = 1; i <= n; i++) scanf("%d", &a[i]), q[i] = a[i]; 30 for(i = 1; i <= m; i++) scanf("%d", &b[i]), q[i + n] = b[i]; 31 std::sort(q + 1, q + n + m + 1); 32 size = std::unique(q + 1, q + n + m + 1) - (q + 1); 33 for(i = 1; i <= n; i++) a[i] = std::lower_bound(q + 1, q + size + 1, a[i]) - q; 34 for(i = 1; i <= m; i++) 35 { 36 b[i] = std::lower_bound(q + 1, q + size + 1, b[i]) - q; 37 p[b[i]] = i; 38 } 39 for(i = 1; i <= n; i++) 40 if(p[a[i]]) 41 { 42 x = query(p[a[i]] - 1) + 1; 43 update(p[a[i]], x); 44 ans = max(ans, x); 45 } 46 printf("%d", ans); 47 return 0; 48 }