【思維+DP】UVA10635 Prince and Princess
阿新 • • 發佈:2021-06-14
分析
如果直接使用LCS
來解決,那麼複雜度為 \(O(pq)\) ,顯然會超時。
因為給出的兩個序列的數都是互不相同的,我們將第一個序列的數按 \(1,2,3...\) 依次編號,然後將第二個序列的數(當然這個數應該是存在於第一個序列的)按照數值對映到相應的編號中,注意到題目所求的LCS
恰好就是第二個數列對應的編號的最長上升子序列(LIS
)長度。
#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #define endl '\n' #define debug(x) cerr << #x << ": " << x << endl #define pb(a) push_back(a) #define set0(a) memset(a,0,sizeof(a)) #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define dwn(i,a,b) for(int i=(a);i>=(b);i--) #define ceil(a,b) (a+(b-1))/b #define INF 0x3f3f3f3f #define ll_INF 0x7f7f7f7f7f7f7f7f typedef long long ll; typedef pair<int,int> PII; typedef pair<double,double> PDD; inline void read(int &x) { int s=0;x=1; char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();} while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar(); x*=s; } const int N=1e5+5; int a[N]; int stk[N], top; int tr[N]; int lowbit(int x){return x&-x;} int query(int p){ int res=-1; for(; p; p-=lowbit(p)) res=max(res, tr[p]); return res; } void modify(int p, int v){ for(; p<N; p+=lowbit(p)) tr[p]=max(tr[p], v); } int main(){ int T; cin>>T; int cases=0; while(T--){ int n, p, q; read(n), read(p), read(q); int t; read(t); int tot=0; map<int, int> mp; rep(i,1,p) read(a[i]), mp[a[i]]=++tot; read(t); top=0; rep(i,1,q){ int k; read(k); if(!mp.count(k)) continue; stk[++top]=mp[k]; } // solve the longest increase sequence set0(tr); int res=0; rep(i,1,top){ int t=query(stk[i])+1; res=max(res, t); modify(stk[i]+1, t); } res++; printf("Case %d: %d\n", ++cases, res); } return 0; }