poj3636--Nested Dolls

Nested Dolls

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7943   Accepted: 2158

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 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

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

Nordic 2007

//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  有没有等号会影响bool  函数中的返回值 ;l相同的w要按照降序排列,l升序排列;

 1 #include <stdio.h>
 2 #include <algorithm>
 3 using namespace std ;
 4
 5 struct dool
 6 {
 7     int l;
 8     int w;
 9 } ;
10 dool num[20020] ;
11
12 bool cmp(dool l, dool w)  //Dirworth定理:l按照升序排序;当l相等时,w也降序,排除了等号关系;
13 {
14     if(l.l == w.l)
15     return l.w > w.w ;
16     else
17     return l.l < w.l ;
18 }
19
20 int main()
21 {
22     int m,n,i ;
23     scanf("%d",&m) ;
24     while(m--)
25     {
26         scanf("%d",&n) ;
27         for(i=0; i<n; i++)
28         scanf("%d %d",&num[i].l, &num[i].w) ;
29         sort(num, num+n, cmp) ;
30
31         int temp, total=0, j ;
32         for(i=0; i<n; i++)
33         {
34             if(num[i].w != 0)
35             {
36                 temp=num[i].w ;
37                 total++ ;
38                 for(j=i+1; j<n; j++)
39                 {
40                     if(num[j].w > temp)
41                     {
42                         temp = num[j].w ;
43                         num[j].w = 0 ;
44                     }
45                 }
46             }
47         }
48         printf("%d\n",total) ;
49     }
50     return 0;
51 }
时间: 2024-08-09 21:51:17

poj3636--Nested Dolls的相关文章

HDU 1677 Nested Dolls

过了之后感觉以前真的做过这种类型的题. 之前一直很疑惑二级排序的优先级问题,现在发现二级排序真的没有绝对的优先级. 对于此题,若按W排序,则有1到i件物品的W均小于等于第i+1件物品(设为A)的W,那么对于第i+1件我们在[1,i]中要选取一个B,使得B.w < A.w && B.h < A.h 且B.h尽可能的大. 这就是所谓的最接近A的B. 因为对于W,后面的均大于等于前面的,所以我们需要一个尽可能大的H. Splay_Tree实现. #include <algori

Nested Dolls (单调递增子序列 + 二分)

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 do

UVA 11368 &amp; POJ 3636 &amp; HDU 1677 Nested Dolls(贪心 + 二分LIS)

A - Nested Dolls Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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

hdu----(1677)Nested Dolls(DP/LIS(二维))

Nested Dolls Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2704    Accepted Submission(s): 802 Problem Description Dilworth is the world’s most prominent collector of Russian nested dolls: he

HDU 1277 Nested Dolls

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1677 题意: 玩俄罗斯套娃,问最后至少还剩几个. 题解: 这题可以和拦截导弹做对比,因为这里是二维的,按w递减h递增的方式来保证在保存的序列中按h升序来排的,从而为二分查找打下基础. 否则,如果按h降序排,保存的序列就会无序,二分结果自然不正确了. 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm>

SPOJ 3943 - Nested Dolls 最长不下降子序列LIS(二分写法)

现在n(<=20000)个俄罗斯套娃,每个都有宽度wi和高度hi(均小于10000),要求w1<w2并且h1<h2的时候才可以合并,问最少能剩几个. [LIS]乍一看跟[这题]类似,但是仔细看是有区别的,其实就相当于上一题多次求LIS,每次求完LIS后把得到的序列删去,然后重新求LIS,最后输出求LIS的次数,我一开始这样写,果然就TLE了.还是要另辟蹊径. 首先用贪心思想,先按照wi从大到小排序,wi相等的情况下hi从小到大,然后求最长不下降子序列(注意可以等于).输出其长度即可. 想

poj 3636 Nested Dolls 动态更新表的二分查找

题意: 给n个玩具,每个有属性w,h.如果w1<w2且h1<h2那么玩具1可放在玩具2里,问最后最少能有几个玩具. 分析: w升序,w相同时h降序排序后是可以贪心的,这里使用了动态维护表的二分算法,表里动态维护了每堆玩具中h的最大值(所以w相同时h要降序).这题我一开始一看是个拓扑图还想着用什么图算法..没想到直接可以贪心,不可以有思维定式啊~~ 代码: //poj 3636 //sep9 #include <iostream> #include <algorithm>

POJ3636Nested Dolls[DP LIS]

Nested Dolls 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 doll

HDU - 1677Nested Dolls最长上升子序列变式

HDU - 1677 Nested Dolls Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description Dilworth is the world's most prominent collector of Russian nested dolls: he literally has thousands of them! You know, the