POJ 3636 Nested Dolls
来源:互联网 发布:maya软件好学吗 编辑:程序博客网 时间:2024/06/07 02:52
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 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
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);}}
- POJ 3636 Nested Dolls
- POJ 3636 Nested Dolls
- POJ 3636 Nested Dolls
- POJ 3636 Nested Dolls
- POJ 3636 俄罗斯Nested Dolls 贪心二分 动态规划
- 【最长非升子序列+nlog(n)】北大 POJ 3636 Nested Dolls
- poj 3636 Nested Dolls 动态更新表的二分查找
- Nested Dolls
- Nested Dolls
- Nested Dolls
- UVA 11368 & POJ 3636 & HDU 1677 Nested Dolls(贪心 + 二分LIS)
- 3636 nested dolls{dilworth}
- hd 1677 Nested Dolls
- zoj 2882 Nested Dolls
- poj3636 Nested Dolls
- 【贪心】poj3636 nested dolls
- HDU1677 Nested Dolls
- HDU1677:Nested Dolls
- 面试问题 一些常用的开发模式
- ubuntu下安装nutch2.x
- Codeforces 706E 十字链表(dancing link)
- Java 设计模式小结
- C++中关于指针的理解
- POJ 3636 Nested Dolls
- C++:Boost库智能指针_scoped_ptr
- next_permutation函数 自动排序
- MySql安装 版本 mysql-5.7.14-winx64
- C++处理字符串的方法——字符串类与字符串变量
- unity3d KeyCode各键值说明
- Java Web学习(35):Filter学习(二)
- POJ 3672(水题)
- Codeforces Round #304 (Div. 2)D. Soldier and Number Game