uva 10635 Prince and Princess(LCS转LIS~)

来源:互联网 发布:营销软件名字 编辑:程序博客网 时间:2024/04/26 16:42

这是个求最长公共子序列的题,最后转化成了最长上升子序列的题,使用了O(nlogn)的算法,贴个讲解的链接

http://blog.csdn.net/shuangde800/article/details/7474903

第一次用lower_bound这个函数,觉得好奇怪,不是要用迭代器的嘛,竟然可以直接用地址,,,呃呃呃反正不太懂

多多练习,,还是不熟啊,都是看题解的

#include <algorithm>#include <cstdio>#include <cstring>#include <iostream>using namespace std;const int maxn = 250 + 10;const int INF = 1000000000;int num[maxn * maxn], princess[maxn * maxn], g[maxn * maxn], d[maxn * maxn];int main(){    int t, n, p, q, i, temp;    scanf("%d", &t);    for (int kase = 1; kase <= t; kase++)    {        scanf("%d%d%d", &n, &p, &q);        memset(num, 0, sizeof(0));        for (i = 1; i <= p + 1; i++)        {            scanf("%d", &temp);            num[temp] = i;        }        int m = 1;        for (i = 1; i <= q + 1; i++)        {            scanf("%d", &temp);            if (num[temp])                princess[m++] = num[temp];        }        for (i = 1; i < m; i++)            g[i] = INF;        int ret = -1;        for (i = 1; i < m; i++)        {            int k = lower_bound(g + 1, g + m, princess[i]) - g;            d[i] = k;            g[k] = princess[i];            ret = max(ret, d[i]);        }        printf("Case %d: %d\n", kase, ret);    }    return 0;}


阅读全文
0 0