2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up Nested Dolls Dilw...

本文详细解析HDU-1677 NestedDolls问题,通过运用Dilworth定理,探讨如何将俄罗斯套娃按尺寸重新组合以减少套娃数量。文章提供了算法思路和具体实现代码,帮助读者理解最长反链和贪心思想在解决此类问题中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目(HDU - 1677)

Nested Dolls

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4020    Accepted Submission(s): 1248

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

分析

如果你还不了解Dilworth定理,那你可以翻阅一下之前的一篇博客

然后再来看一看这道题目。题目很友好的一上来就告诉我们有一个叫Dilworth的人收集套娃。如果你知道这个定理,那这个提醒就不能再明显了啊。题目要求最后的最少分成几组套娃。这显然是求链的个数。那么根据Dilworth定理,我们要求的是其最长反链的长度。如果你眼尖,可能看出这道题跟木棍加工很像。但又有一些区别,比如,木棍允许宽度和长度中只要有一个满足即可,但是套娃必须宽度小于前一个套娃,高度也小于前一个套娃才能套进去。

排序函数也要做一些调整。这里是先按照一个做升序排列,如果这个关键字相同,那么按另一个关键字降序排列。其实这是一个贪心的思想,为了能套进更多的套娃,在其中一个关键字相同时,另一个关键字越大,就更有可能套进多的套娃。另外的微调即可,包括LIS中要存储两个关键字等。

程序

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int Max_Stick=20000+10;
 4 
 5 struct Node{int Length,Weight;}Stick[Max_Stick];
 6 Node LIS[Max_Stick];
 7 int N_Stick,Len_LIS=0;
 8 bool Compare(Node X,Node Y)
 9 {
10       if(X.Length!=Y.Length)
11           return X.Length<Y.Length;
12       return X.Weight>Y.Weight;
13 }
14 
15 int main()
16 {
17     int i,j;
18     int t;
19     cin >> t;
20     while(t--)
21     {
22         memset(Stick,0,sizeof(Stick));
23         memset(LIS,0,sizeof(LIS));
24         Len_LIS=0;
25         scanf("%d",&N_Stick);
26         for (i=0;i<N_Stick;i++)
27                scanf("%d%d",&Stick[i].Length,&Stick[i].Weight);
28         sort(Stick,Stick+N_Stick,Compare);
29           for(i=0;i<N_Stick;i++)
30            {
31             int Flag=false;
32             for (j=0;j<Len_LIS;j++)
33                   if (LIS[j].Length<Stick[i].Length&&LIS[j].Weight<Stick[i].Weight)
34                 {
35                     LIS[j].Length=Stick[i].Length;
36                     LIS[j].Weight=Stick[i].Weight;
37                       Flag=true;
38                     break;
39                 }
40              if (!Flag) 
41               {
42                   LIS[Len_LIS].Length=Stick[i].Length;
43                 LIS[Len_LIS].Weight=Stick[i].Weight;
44                    Len_LIS++;
45               } 
46            }    
47          printf("%d\n",Len_LIS);
48     }
49      return 0;
50 }

 

转载于:https://www.cnblogs.com/OIerPrime/p/8387578.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值