[USACO17FEB]Why Did the Cow Cross the Road II P

嘟嘟嘟

考虑dp。

对于ai,和他能匹配的bj只有9个,所以我们考虑从这9个状态转移。

对于ai 能匹配的一个bj,当前最大的匹配数一定是[1, j - 1]中的最大匹配数 + 1。然后用树状数组维护前缀匹配数最大值就行了。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<cctype>
 8 #include<vector>
 9 #include<stack>
10 #include<queue>
11 using namespace std;
12 #define enter puts("") 
13 #define space putchar(' ')
14 #define Mem(a, x) memset(a, x, sizeof(a))
15 #define rg register
16 typedef long long ll;
17 typedef double db;
18 const int INF = 0x3f3f3f3f;
19 const db eps = 1e-8;
20 const int maxn = 1e5 + 5;
21 inline ll read()
22 {
23   ll ans = 0;
24   char ch = getchar(), last = ' ';
25   while(!isdigit(ch)) {last = ch; ch = getchar();}
26   while(isdigit(ch)) {ans = ans * 10 + ch - '0'; ch = getchar();}
27   if(last == '-') ans = -ans;
28   return ans;
29 }
30 inline void write(ll x)
31 {
32   if(x < 0) x = -x, putchar('-');
33   if(x >= 10) write(x / 10);
34   putchar(x % 10 + '0');
35 }
36 
37 int n, a[maxn], pos[maxn];
38 int Max[maxn];
39 
40 int c[maxn];
41 int lowbit(int x)
42 {
43   return x & -x;
44 }
45 void add(int pos, int x)
46 {
47   for(; pos <= n && c[pos] < x; pos += lowbit(pos)) c[pos] = x;
48 }
49 int query(int pos)
50 {
51   int ret = 0;
52   for(; pos; pos -= lowbit(pos)) if(c[pos] > ret) ret = c[pos];
53   return ret;
54 }
55 
56 int main()
57 {
58   n = read();
59   for(int i = 1; i <= n; ++i) a[i] = read();
60   for(int i = 1; i <= n; ++i) {int x = read(); pos[x] = i;}
61   for(int i = 1; i <= n; ++i)
62     {
63       for(int j = max(1, a[i] - 4); j <= min(n, a[i] + 4); ++j)
64     Max[j] = query(pos[j] - 1);
65       for(int j = max(1, a[i] - 4); j <= min(n, a[i] + 4); ++j)
66     add(pos[j], Max[j] + 1);
67     }
68   write(query(n)), enter;
69   return 0;
70 }
View Code

猜你喜欢

转载自www.cnblogs.com/mrclr/p/9837987.html