POJ 3636 Nested Dolls

来源:互联网 发布:maya软件好学吗 编辑:程序博客网 时间:2024/06/07 02:52
Nested Dolls
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8301 Accepted: 2258

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 andh1 < 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 w1h1,w2h2, ... ,wmhm, where wi is the width and hi is the height of doll number i. 1 ≤ wihi ≤ 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

4320 30 40 50 30 40420 30 10 10 30 20 40 50310 30 20 20 30 10410 10 20 30 40 50 39 51

Sample Output

1232

Source

Nordic 2007


题意:给你n个娃娃的h(高度),w(宽度),如果一个娃娃的h和w都分别严格小于另一个娃娃的h和w,第一个娃娃就可以套进第二个娃娃

多个娃娃套在一起组成的娃娃被看成一个娃娃

请你求出得到娃娃的最少数量

没有我开始想象的那么简单(开始以为dp)

正解是这样,首先按照每个娃娃的w升序排序,如果w相等就按照h降序排序,那么我们就要找出对于h的最少单调递增子序列的个数

求子序列的个数用到了类似于dp的方法,先定义一个数组b,里面存的是每个单调递增子序列的最大元素,也就是说b数组里元素数量就是求出的子序列个数

依次枚举每一个娃娃在b中二分查找判断有没有比当前娃娃的h值还要小的,有就将当前h换成最大元素,否则就将h添加到b数组中

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;const int maxn=2e4+5,inf=1e9;int t,n,b[maxn];struct wk{int w,h;}s[maxn];bool cmp(wk a,wk b){return a.w==b.w?a.h>b.h:a.w<b.w;}int main(){cin>>t;while(t--){scanf("%d",&n);memset(s,0,sizeof(s));memset(b,0,sizeof(b));int i,pos,len=1;for(i=1;i<=n;i++)    scanf("%d%d",&s[i].w,&s[i].h);sort(s+1,s+1+n,cmp);b[1]=s[1].h;for(i=2;i<=n;i++){int l=1,r=len;while(l<=r){int mid=(l+r)>>1;if(b[mid]>=s[i].h)l=mid+1;else r=mid-1;}b[l]=s[i].h;if(l>len)len++;}printf("%d\n",len);}}


0 0
原创粉丝点击