zoukankan      html  css  js  c++  java
  • BZOJ1264 [AHOI2006]基因匹配Match 【LCS转LIS】

    题目链接

    BZOJ1264

    题解

    平凡的(LCS)(O(n^2))
    显然我们要根据题目的性质用一些不平凡的(LCS)求法
    这就很巧妙了,,

    我们考虑(A)序列的每个位置可能匹配(B)位置的哪些位置
    (A)序列中匹配的位置一定是单调递增的
    那么我们就把(A)的每个位置所能匹配(B)的位置找出来,降序排列替代(A)原来的位置
    我们就能得到一个新的序列,显然原序列的(LCS)就是新序列的(LIS)
    而由于题目的限制,新序列只能使原序列长度的(5)
    而求(LIS)(O(nlogn))
    就可以(A)

    比如
    A:1 1 1 2 2 2 1 2 2 1
    B:1 1 1 1 1 2 2 2 2 2
    1对应5 4 3 2 1
    2对应10 9 8 7 6
    那么新序列 5 4 3 2 1,5 4 3 2 1,5 4 3 2 1,10 9 8 7 6,10 9 8 7 6,10 9 8 7 6,.......

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<map>
    #include<vector>
    #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
    #define REP(i,n) for (int i = 1; i <= (n); i++)
    #define mp(a,b) make_pair<int,int>(a,b)
    #define cls(s) memset(s,0,sizeof(s))
    #define cp pair<int,int>
    #define LL long long int
    using namespace std;
    const int maxn = 500005,maxm = 100005,INF = 1000000000;
    inline int read(){
    	int out = 0,flag = 1; char c = getchar();
    	while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
    	while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    	return out * flag;
    }
    vector<int> pos[maxn];
    int n,A[maxn],B[maxn],C[maxn],bac[maxn],f[maxn],N,M;
    int cal(int x){
    	int l = 0,r = N,mid;
    	while (l < r){
    		mid = l + r + 1 >> 1;
    		if (bac[mid] < x) l = mid;
    		else r = mid - 1;
    	}
    	return l;
    }
    int main(){
    	n = read(); N = n * 5;
    	REP(i,N) A[i] = read();
    	REP(i,N) B[i] = read(),pos[B[i]].push_back(i);
    	REP(i,N){
    		for (unsigned int j = pos[A[i]].size() - 1; ~j; j--)
    			C[++M] = pos[A[i]][j];
    	}
    	memset(bac,0x3f3f3f3f,sizeof(bac)); bac[0] = 0;
    	int ans = 0;
    	REP(i,M){
    		f[i] = cal(C[i]) + 1;
    		bac[f[i]] = min(bac[f[i]],C[i]);
    		ans = max(ans,f[i]);
    	}
    	printf("%d
    ",ans);
    	return 0;
    }
    
    
  • 相关阅读:
    DB2数据常用指令
    HTMLParser使用
    面试知识点总结之数据库
    面试知识点总结之算法
    面试知识点总结之操作系统
    面试知识点总结之计算机网络
    面试知识点总结之Java语言
    一个精确匹配的问题
    【转】矩阵求导,矩阵计算
    【转】用C语言扩展Python的功能
  • 原文地址:https://www.cnblogs.com/Mychael/p/9200888.html
Copyright © 2011-2022 走看看