POJ3636Nested Dolls[DP LIS]
Posted Candy?
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3636Nested Dolls[DP LIS]相关的知识,希望对你有一定的参考价值。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8323 | Accepted: 2262 |
Description
Dilworth is the world‘s most prominent collector of Russian nested dolls: he literally has thousands of them! You know, the wooden hollow dolls of different sizes of which the smallest doll is contained in the second smallest, and this doll is in turn contained in the next one and so forth. One day he wonders if there is another way of nesting them so he will end up with fewer nested dolls? After all, that would make his collection even more magnificent! He unpacks each nested doll and measures the width and height of each contained doll. A doll with width w1 and height h1 will fit in another doll of width w2 and height h= if and only if w1 < w2 and h1 < h2. Can you help him calculate the smallest number of nested dolls possible to assemble from his massive list of measurements?
Input
On the first line of input is a single positive integer 1 ≤ t ≤ 20 specifying the number of test cases to follow. Each test case begins with a positive integer 1 ≤ m ≤ 20000 on a line of itself telling the number of dolls in the test case. Next follow 2m positive integers w1, h1,w2, h2, ... ,wm, hm, where wi is the width and hi is the height of doll number i. 1 ≤ wi, hi ≤ 10000 for all i.
Output
For each test case there should be one line of output containing the minimum number of nested dolls possible.
Sample Input
4 3 20 30 40 50 30 40 4 20 30 10 10 30 20 40 50 3 10 30 20 20 30 10 4 10 10 20 30 40 50 39 51
Sample Output
1 2 3 2
Source
// // main.cpp // poj3636 // // Created by abc on 16/8/30. // Copyright © 2016年 abc. All rights reserved. // #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int N=20005,INF=1e9; struct data{ int w,h; }da[N]; bool cmpda(data a,data b){ if(a.w>b.w) return 0; if(a.w<b.w) return 1; if(a.w==b.w) return a.h>b.h?1:0; return 1; } int t,n; int f[N],g[N],a[N]; bool cmp(int a,int b){ return a>b; } int dp(){ int ans=0; sort(da+1,da+1+n,cmpda); memset(f,0,sizeof(f)); for(int i=1;i<=n;i++) g[i]=-INF,a[i]=da[i].h; for(int i=1;i<=n;i++){ int k=upper_bound(g+1,g+1+n,a[i],cmp)-g; f[i]=k; g[k]=a[i]; ans=max(ans,f[i]); } return ans; } int main(int argc, const char * argv[]) { scanf("%d",&t); for(int i=1;i<=t;i++){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d%d",&da[i].w,&da[i].h); printf("%d\n",dp()); } return 0; }
以上是关于POJ3636Nested Dolls[DP LIS]的主要内容,如果未能解决你的问题,请参考以下文章
POJ 1769 Minimizing maximizer(DP+zkw线段树)