MDOLLS - Nested Dolls

no tags 

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? 


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.

20 30 40 50 30 40
20 30 10 10 30 20 40 50
10 30 20 20 30 10
10 10 20 30 40 50 39 51


For each test case there should be one line of output containing the minimum
number of nested dolls possible.


hide comments
anmolx123: 2017-07-24 22:10:03

How is the third Test case giving output 3??

amiri: 2017-06-22 08:47:34

There is an beautiful O(n log n) solution using LIS

Last edit: 2017-06-22 14:15:00
candide: 2017-05-01 19:27:21

Solved in C and Python with binary search. Didn't try maximum bipartite matching.

Last edit: 2017-05-01 21:16:26
C├ęsar Giglio Badoino: 2016-04-03 05:23:01

@naruto09 2 dolls

naruto09: 2016-01-16 09:03:44

i/o :
72 85 61 86 55 85 43 63 65 39 44 30
can someone please tell output of this test case..?

Last edit: 2016-01-16 09:03:56
sughosh v kaushik: 2015-08-15 14:03:09

greedy is good but with binary search this time :)

Last edit: 2015-08-15 14:20:02
Andrei Dinu: 2014-10-30 20:04:08

Can you please provide more tests? I made a solution with sorting and longest increasing subsequence, but I don't see where I am wrong. I get wrong answer starting with test no. 3.

Rishav Goyal: 2014-07-01 16:34:49

nice problem. O(nlogn) is optimal for sure does not matter nlogn, 2nlogn, 3nlogn. costant factor will not make tle.

যোবায়ের: 2013-03-29 19:12:59

@PubLic_AvenGeR (nlogn+nlogn) is optimal I think.

Added by:~!(*(@*!@^&
Time limit:0.156s-0.259s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:Nordic 2007