zoukankan      html  css  js  c++  java
  • UVA10635 Prince and Princess

    传送门

    题目大意:

    给两个序列,数均在([1, n*n]),求最长公共子序列。

    题目分析:

    若用传统的(LCS)显然会炸,因为数字均在([1, n * n]),若序列a的数的位置(1~n),数列b变为b[i]在a中出现的位置,求出LIS即可。(o(n log n))

    code

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<string>
    //#include<vector>
    using namespace std;
    
    const int N = 1000, OO = 0x3f3f3f3f;
    int T, a[N * N], b[N * N], has[N * N], f[N * N], n, p, q;
    
    int main(){
        scanf("%d", &T);
        int k = 0;
        while(T--){
            scanf("%d%d%d", &n, &p, &q);
            memset(has, 0, sizeof has);
            for(int i = 1; i <= p + 1; i++) scanf("%d", &a[i]), has[a[i]] = i;
            for(int i = 1; i <= q + 1; i++) scanf("%d", &b[i]), b[i] = has[b[i]];
            // for(int i = 1; i <= q + 1; i++) cout << b[i] << " "; cout<<endl;
            memset(f, OO, sizeof f);
            for(int i = 1; i <= q + 1; i++){
                if(!b[i]) continue;
                *lower_bound(f + 1, f + q + 1 + 1, b[i]) = b[i];
            }
            // for(int i = 1; i <= q + 1; i++) cout << f[i] << " "; cout<<endl;
            printf("Case %d: %d
    ", ++k, lower_bound(f + 1, f + q + 1 + 1, OO) - (f + 1));
        }
    }
    
  • 相关阅读:
    清除浮动解决父元素高度塌陷问题
    canvas画动图
    vue实现列表的循环滚动
    localStorage读写操作
    angularJS快速入门
    python模块
    python函数式编程
    python高级特性
    Flask 快速入门
    JQuery Ajax
  • 原文地址:https://www.cnblogs.com/CzYoL/p/7658921.html
Copyright © 2011-2022 走看看