码迷,mamicode.com
首页 > 其他好文 > 详细

uva 10635 LCS转LIS

时间:2016-07-13 01:02:40      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

这道题两个数组都没有重复的数字,用lcs的nlogn再适合不过了

 

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 1005
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f

int n,m,v;

int q,p,top;

int a[250*250],b[250*250],c[250*250],pos[250*250],stk[250*250];

void lis(int len)
{
    mem(stk,0);
    top = 0;
    stk[0] = -1;
    for(int i=1;i<=len;i++)
    {
        if(c[i]>stk[top])
        {
            stk[++top] = c[i];
        }
        else
        {
            int lo=1,hi=top,mid;
            while(lo<=hi)
            {
                mid = (lo+hi)>>1;
                if(c[i]>stk[mid]) lo = mid+1;
                else hi = mid -1;
            }
            stk[lo] = c[i];
        }
    }
}

int main()
{
    int i,j;
    int t,kase=1;
    sf("%d",&t);
    while(t--)
    {
        sf("%d%d%d",&n,&p,&q);
        mem(pos,0);
        mem(c,0);
        v=1;
        for(i=1;i<=p+1;i++)
        {
            sf("%d",&a[i]);
            pos[a[i]] = i;
        }
        for(i=1;i<=q+1;i++)
        {
            sf("%d",&b[i]);
            if(pos[b[i]]) c[v++] = pos[b[i]];
        }
        lis(v);
        pf("Case %d: %d\n",kase++,top);
    }
}

 

uva 10635 LCS转LIS

标签:

原文地址:http://www.cnblogs.com/qlky/p/5665382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!