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
kumarpritam863: 2020-01-27 18:06:33

beautiful O(nlogn) solution

rishabh_jiit: 2019-03-11 14:57:46

Those who are unable to understand the question. Read the question from here.

egoista_: 2018-05-21 07:48:48

Great Problem. Learnt Dilworth's Algorithm and implementd using largest non-increasing subsequence.

shashankpathak: 2018-03-20 07:41:54

How to approach this problem using Dilsworth theorem??
Thanks in advance

biradarganesh: 2018-01-02 06:28:49

Can someone please explain the problem? I've been trying for an hour to understand the problem, but I'm unable to even understand what they're asking us to do!

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

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