搜索
您的当前位置:首页Uva - 10635 - Prince and Princess(LCS转LIS)

Uva - 10635 - Prince and Princess(LCS转LIS)

来源:乌哈旅游

题意:一个n*n的棋盘(2 <= n <= 250),一个p+1个数的数组,各个数互不相同,第一个数是1,最后 一个数是n*n;一个q+1个数的数组,各个数互不相同,第一个数是1,最后 一个数是n*n;1 <= p,q < n*n;问这两个数组的LCS。

题目链接:

——>>把第一个序列重新编号为:1, 2, 3, ...

则第二个序列相应地就成了求LIS。

#include <cstdio>
#include <algorithm>
#include <cstring>

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;
}


因篇幅问题不能全部显示,请点此查看更多更全内容

Top