这道题两个数组都没有重复的数字,用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); } }
时间: 2024-10-10 21:43:29