【LCS+树状数组】BZOJ1264 [AHOI2006]基因匹配Match
    
  
      
      
     
    
      
        题面在这里
考虑LCS的DP
先滚动数组,然后发现每个位置就是前缀最小值
因为每个数只出现5次,所以对于每个\(i\),只有5个位置要更新
树状数组维护之
示例程序:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 
 | #include<cstdio>#include<vector>
 #include<algorithm>
 using namespace std;
 
 const int maxn=100005;
 int n,N,a[maxn],b[maxn];
 vector<int> pos[maxn];
 int BIT[maxn];
 #define lowbit(x) ((x)&-(x))
 void ist(int x,int w){
 for (int i=x;i<=N;i+=lowbit(i))
 BIT[i]=max(BIT[i],w);
 }
 int qry(int x){
 int res=0;
 for (int i=x;i;i-=lowbit(i))
 res=max(res,BIT[i]);
 return res;
 }
 int main(){
 scanf("%d",&n);N=n*5;
 for (int i=1;i<=N;i++) scanf("%d",&a[i]);
 for (int i=1;i<=N;i++) scanf("%d",&b[i]),pos[b[i]].push_back(i);
 for (int i=1;i<=N;i++)
 for (int j=4;j>=0;j--)
 ist(pos[a[i]][j],qry(pos[a[i]][j]-1)+1);
 printf("%d",qry(N));
 return 0;
 }
 
 |