1. 程式人生 > >[Usaco2017 Feb]Why Did the Cow Cross the Road II (Platinum)

[Usaco2017 Feb]Why Did the Cow Cross the Road II (Platinum)

Description
Farmer John is continuing to ponder the issue of cows crossing the road through his farm, introduced in the preceding problem. He realizes that interaction between some pairs of breeds is actually acceptable if the breeds are friendly, a property that turns out to be easily characterized in terms of breed ID: breeds aa and bb are friendly if |a-b|≤4, and unfriendly otherwise. It is ok for cows to wander into fields designated for other breeds, as long as they are friendly.Given the ordering of N fields on both sides of the road through FJ's farm (again, with exactly one field for each breed on each side), please help FJ determine the maximum number of crosswalks he can draw over his road, such that no two intersect, and such that each crosswalk joins a pair of fields containing two breeds that are friendly. Each field can be accessible via at most one crosswalk (so crosswalks don't meet at their endpoints).
上下有兩個長度為n、位置對應的序列A、B,其中數的範圍均為1~n。若abs(A[i]-B[j]) <= 4,則A[i]與B[j]間可以連一條邊。現要求在邊與邊不相交的情況下的最大的連邊數量。n <= 10^6

Input
The first line of input contains N (1≤N≤100,0000).
The next N lines describe the order, by breed ID, of fields on one side of the road;
each breed ID is an integer in the range 1…N The last N lines describe the order, by breed ID, of the fields on the other side of the road.
Each breed ID appears exactly once in each ordering.
注意:兩個序列都是全排列

Output
Please output the maximum number of disjoint "friendly crosswalks" Farmer John can draw across the road.

Sample Input
6
1
2
3
4
5
6
6
5
4
3
2
1

Sample Output
5

首先可以想到二維dp,設\(f[i][j]\)表示序列A的前\(i\)個數和序列B的前\(j\)個數的最大連邊數,那麼就有

\[f[i][j]=max\{f[i-1][j],f[i][j-1],(f[i-1][j-1]+1)*[|A_i-B_j|<=4]\}\]

但是這樣轉移會TLE,於是我們需要給\(f[i][j]\)增加一個限制條件,即\(A_i\)\(B_j\)之間有連線,這樣的話轉移即為\(f[i][j]=max(f[x][y]+1),(1<x<i,1<y<j)\),這樣轉移是需要字首最大值的,於是就可以使用樹狀陣列進行優化,而且第二維可以省去

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
#define lowbit(x) ((x)&(-x))
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
    static char buf[1000000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
    int x=0,f=1;char ch=gc();
    for (;ch<'0'||ch>'9';ch=gc())   if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<1)+(x<<3)+ch-'0';
    return x*f;
}
inline int read(){
    int x=0,f=1;char ch=getchar();
    for (;ch<'0'||ch>'9';ch=getchar())  if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())    x=(x<<1)+(x<<3)+ch-'0';
    return x*f;
}
inline void print(int x){
    if (x<0)    putchar('-'),x=-x;
    if (x>9)    print(x/10);
    putchar(x%10+'0');
}
const int N=1e5;
int v[N+10],pos[N+10],now[N+10],tree[N+10],n;
void insert(int x,int v){for (;x<=n;x+=lowbit(x))   tree[x]=max(tree[x],v);}
int Query(int x){
    if (!x) return 0;
    int res=0;
    for (;x;x-=lowbit(x))   res=max(res,tree[x]);
    return res;
}
int main(){
    n=read();
    for (int i=1;i<=n;i++)  v[i]=read();
    for (int i=1;i<=n;i++)  pos[read()]=i;
    for (int i=1;i<=n;i++){
        for (int j=max(1,v[i]-4);j<=min(n,v[i]+4);j++)  now[j]=Query(pos[j]-1);
        for (int j=max(1,v[i]-4);j<=min(n,v[i]+4);j++)  insert(pos[j],now[j]+1);
    }
    printf("%d\n",Query(n));
    return 0;
}