【模板】最长公共子序列

Code:(中秋模拟)

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<queue>
 6 #define ms(a,b) memset(a,b,sizeof a)
 7 #define rep(i,a,n) for(int i = a;i <= n;i++)
 8 #define per(i,n,a) for(int i = n;i >= a;i--)
 9 #define inf 2147483647
10 using namespace std;
11 typedef long long ll;
12 ll read() {
13     ll as = 0,fu = 1;
14     char c = getchar();
15     while(c < '0' || c > '9') {
16         if(c == '-') fu = -1;
17         c = getchar();
18     }
19     while(c >= '0' && c <= '9') {
20         as = as * 10 + c - '0';
21         c = getchar();
22     }
23     return as * fu;
24 }
25 const int N = 300005;
26 //head
27 int n,m;
28 int a[N],b[N],t[N<<1];
29 struct lsh {
30     int x,id;
31     bool operator < (const lsh &o) const {
32         return x < o.x;
33     }
34 } tmp[N<<1];
35 int cur;
36 
37 int main() {
38     n = read();
39     m = read();
40     rep(i,1,n) a[i] = read(),tmp[i] = (lsh) {
41         a[i],i
42     };
43     rep(i,1,m) b[i] = read(),tmp[n+i] = (lsh) {
44         b[i],i+n
45     };
46     sort(tmp+1,tmp+n+m+1);
47     tmp[0].x = -inf;
48     rep(i,1,n+m) {
49         t[tmp[i].id] = t[tmp[i-1].id] + (tmp[i].x > tmp[i-1].x);
50     }
51     rep(i,1,n+m) i <= n ? a[i] = t[i] : b[i-n] = t[i];
52     ms(t,0);
53     rep(i,1,n) if(!t[a[i]]) t[a[i]] = i;
54     rep(i,1,m) b[i] = t[b[i]];
55     int top = 0;
56     rep(i,1,m) b[i] == 0 ? 0 : b[++top] = b[i];
57     m = top;
58     ms(t,0),top = 0;
59     rep(i,1,m) {
60         if(b[i] >= t[top]) t[++top] = b[i];
61         else {
62             int k = upper_bound(t+1,t+1+top,b[i]) - t;
63             t[k] = min(t[k],b[i]);
64         }
65     }
66     printf("%d\n",top);
67     return 0;
68 }
posted @ 2018-10-03 22:50  白怀潇  阅读(178)  评论(0)    收藏  举报