UVA10635 Prince and Princess 动态规划

来源:互联网 发布:python为什么叫爬虫 编辑:程序博客网 时间:2024/05/17 23:55

刷刷水题找手感

题目大意:有一个n*n的矩阵,从左到右从上到下把格子编号为1,2,……n*n,有两个人从要从左上角格子1采取不同的行动路线到达右下角格子n*n。移动路线除了不能重复外没有限制,因此最终路线是1,2,3……p的一个排列(p是走了几步)
现在给出这两个人的行动序列,求出通过去除路径上的点,两人能一起行走的最长路线。

乍一看是LCS,但是空间超,就算优化了空间也会超时间。
注意到题目中的序列是无重复的数字,从这里下手进行优化。

考虑答案中的最长路线,肯定仅有两个人都走过的点,因此去除两个人走的点中另外一个人没走过的。
留下两串等长序列,再将两人最终路线中的点建立关系,可以发现其对应关系是一个上升子序列。因此问题转化为求LIS的问题。

#pragma comment(linker, "/STACK:102400000,102400000")#include<iostream>#include<iomanip>#include<cstdio>#include<cstring>#include<sstream>#include<stack>#include<queue>#include<fstream>#include<algorithm>#include<map>#include<set>#include<vector>#include<functional>#include<cmath>#define CLR(x) memset(x,0,sizeof(x))#define SETMAX(x) memset(x,0x3f,sizeof(x))#define SETNO(x) memset(x,-1,sizeof(x))#define ll long long#define eps 3e-12#define pow2(x) ((x)*(x))#define forto(i,n) for(int i=0;i<n;i++)#define for1to(i,n) for(int i=1;i<=n;i++)#define VI vector<int>using namespace std;const double PI=acos(-1.0);#define INF 0x3f3f3f3f#define NINF 0xbfffffffusing namespace std;int Apos[255*255],Bpos[255*255];int LIS[255*255];int LISLen;int main(){    ios_base::sync_with_stdio(false);    int T;    scanf("%d",&T);    for1to(C,T)    {        int n,p,q;        scanf("%d%d%d",&n,&p,&q);        p++;        q++;        CLR(Apos);        CLR(Bpos);        for1to(i,p)        {            int t;            scanf("%d",&t);            Apos[t]=i;        }        for1to(i,q)        {            int t;            scanf("%d",&t);            Bpos[i]=Apos[t];        }        LISLen=0;        LIS[LISLen++]=Bpos[1];        for(int i=2;i<=q;i++)        {            if (Bpos[i]==0)                continue;            if (Bpos[i]>LIS[LISLen-1])                LIS[LISLen++]=Bpos[i];            else                *lower_bound(LIS,LIS+LISLen,Bpos[i])=Bpos[i];        }        printf("Case %d: %d\n",C,LISLen);    }    return 0;}
0 0
原创粉丝点击