链接:



Nested Dolls

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1876    Accepted Submission(s): 524

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
 
Source
 
Recommend
lcy


题意:


      给你 N 个娃娃, 每个娃娃有特定的 w 和 h

      当且仅当 d1.w < d2.w && d1.h < d2.h , d1 才可以放入 d2中

      问:最少还可以剩下几个娃娃

算法:应该是要用 dp 做了,这里用贪心Orz

最后发现都一样,贪心还是 DP , 分析了 hdu 1257 就知道了Orz

思路:


      感觉类似于hdu 1257 最少拦截系统  的非DP解法【贪心求解】 hdu
1257 题解


      先对娃娃们排序:先按照 w 从大到小排序, w 相同,则按照 h 从小到大排序

关于排序:


           先按照 w 从大到小排序可以理解吧【把小的嵌套到大的当中去】

           对于 w 相同时 h 从小到大排序

           如果 m 个 w 相同, 那么必然是嵌套在 m 个不同的娃娃中

          【可能是比它大的,也可能是它本身】

           先选择 h 最小的嵌入到前面能够满足条件的娃娃中,

           再新用一个娃娃嵌套 h 倒数第二小的娃娃,

           那么这时嵌套了第二个娃娃的东西,一定能比已经嵌套了第个一的更能嵌套其它的娃娃 w 相同 ,而 h 更优【h大】
         
           比如说这堆娃娃有这样几个娃娃

           5

           30 400  (1)

           10 200  (1)

           10 300  (2) w 相同,必然重新嵌入不同的娃娃

           9 250   (2)

           8 250   (3)

           那么最优的结果就是 3,

           第一个娃娃嵌套第二个;

           第三个娃娃嵌套第四个;

           第五个娃娃单独嵌套。





           但是如果你按照 w 相同时 h 从大到小排序就是这样

           5

           30 400  (1)

           10 300  (1)

           10 200  (2)

           9 250   (3)

           8 250   (4)

           一样的数据, 答案是 4

code:

/*****************************************************************************************

E	Accepted	540 KB	343 ms	C++	1172 B	2013-08-04 21:16:03

题意:给你 N 个娃娃, 每个娃娃有特定的 w 和 h
当且仅当 d1.w < d2.w && d1.h < d2.h , d1 才可以放入 d2中
问:最少还可以剩下几个娃娃
算法:应该是要用 dp 做了,这里用贪心Orz
思路:感觉类似于导弹拦截系统
先对娃娃们排序:先按照 w 从大到小排序, w 相同,则按照 h 从小到大排序
关于排序:先按照 w 从大到小排序可以理解吧【把小的嵌套到大的当中去】
对于 w 相同时 h 从小到大排序
如果 m 个 w 相同, 那么必然是嵌套在 m 个不同的娃娃中
【可能是比它大的,也可能是它本身】
先选择 h 最小的嵌入到前面能够满足条件的娃娃中,
再新用一个娃娃嵌套 h 倒数第二小的娃娃,
那么这时嵌套了第二个娃娃的东西,一定能比已经嵌套了第个一的更能嵌套其它的娃娃 w 相同 ,而 h 更优【h大】
比如说这堆娃娃有这样几个娃娃
5
30 400 (1)
10 200 (1)
10 300 (2) w 相同,必然重新嵌入不同的娃娃
9 250 (2)
8 250 (3)
那么最优的结果就是 3,
第一个娃娃嵌套第二个;
第三个娃娃嵌套第四个;
第五个娃娃单独嵌套。 但是如果你按照 w 相同时 h 从大到小排序就是这样
5
30 400 (1)
10 300 (1)
10 200 (2)
9 250 (3)
8 250 (4)
一样的数据, 答案是 4 *****************************************************************************************/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std; const int maxn = 20000+10;
const int INF = 10000+10; struct Node
{
int w,h;
}node[maxn], dp[maxn]; bool cmp(Node a, Node b)
{
if(a.w == b.w) return a.h < b.h;
else return a.w > b.w;
} int main()
{
int T;
int n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%d%d", &node[i].w, &node[i].h);
dp[i].w = dp[i].h = INF; //假设初始化有 N 个可以嵌套的娃娃
}
sort(node, node+n, cmp);
//for(int i = 0; i < n; i++) printf("%d %d\n", node[i].w, node[i].h); printf("\n"); for(int i = 0; i < n; i++)
{
int j = 0;
while(dp[j].w <= node[i].w || dp[j].h <= node[i].h) j++;
dp[j].w = node[i].w; //不断更新为当前情况
dp[j].h = node[i].h;
} // for(int i = 0; i < n; i++) printf("%d %d\n", dp[i].w, dp[i].h); printf("\n"); int ans = 0;
for(int i = 0; i < n; i++)
if(dp[i].h != INF) //看有几个嵌套过
ans++; printf("%d\n", ans);
}
return 0;
}




hdu 1677 Nested Dolls【贪心解嵌套娃娃问题】的更多相关文章

  1. HDU 1677 Nested Dolls

    过了之后感觉曾经真的做过这样的类型的题. 之前一直非常疑惑二级排序的优先级问题,如今发现二级排序真的没有绝对的优先级. 对于此题,若按W排序,则有1到i件物品的W均小于等于第i+1件物品(设为A)的W ...

  2. Nested Dolls 贪心 + dp

    G: Nested Dolls Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 99     Solved: 19 Descript ...

  3. HDU 1277 Nested Dolls

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1677 题意: 玩俄罗斯套娃,问最后至少还剩几个. 题解: 这题可以和拦截导弹做对比,因为这里是二维的 ...

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

    Nested Dolls Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  5. 子查询解嵌套in改写为exists

    SELECT * FROM (SELECT pubformdat0_.id id332_, pubformdat0_.domain_id domain2_332_, pubformdat0_.proc ...

  6. HDU 1677

    Nested Dolls Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. HDU 4442 Physical Examination(贪心)

    HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...

  8. [LeetCode] Flatten Nested List Iterator 压平嵌套链表迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  9. [LeetCode] Nested List Weight Sum 嵌套链表权重和

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

随机推荐

  1. AssionShop开源B2C电子商务系统-(一)用例图(转载)

    这篇文章,将有实质的进展.先把大体的需求整理了一份用例图,自认为粒度做的已经很细了,再细就没法搞了.我还是坚信一个原则:自己业余搞的东西千万不要 想一口吃个胖子,这样很容易项目就搞流产了~,废话不多说 ...

  2. 打败 IE 的葵花宝典:CSS Bug Table

    博主说:本博客文章来源包括转载,翻译,原创,且在文章内均有标明.鼓励原创,支持创作共享,请勿用于商业用途,转载请注明文章链接.本文链接:http://www.kein.pw/?p=35 原文发表于:A ...

  3. Atitit.预定义变量与变量预处理器

    Atitit.预定义变量与变量预处理器 1. 预定义变量与1 2. 变量预处理器1 3. 测试数据生成器3 1. 预定义变量与 姓名:$name 次数:$rdm 时间:$datetime 文件名:$f ...

  4. Ubuntu 的 apt-get 代理设置(zhuan)

    url: http://qixinglu.com/post/ubuntu_apt-get_proxy_setup.html 升级到 Ubuntu10.04 后,发现 apt-get 的代理设置有改变了 ...

  5. InnoDB:表

    数据在表中是如何进行组织存放的?下面我们就来看看: InnoDB引擎表的类型 InnoDB表都会有一个主键. 如果没有显示的指定主键,首先会去查找,看是否有非空的唯一索引, 如果有,则该列为主键:如果 ...

  6. Activiti(一)--安装配置具体解释

    有一段时间没有更新文章了,尽管有一直在写文章,但是一直没有更新到博客内,这段时间写的文章大多还是以技术为主. 接下来的系列文章将会来讨论企业工作流的开发,主要是来研究开源工作流Activiti的使用. ...

  7. powerDesigner建表时选择不同数据库类型

    l  使用powerDesigner创建表模型的时候可选择数据库类型 如图设置为mysql:

  8. IT 服务管理工具 iTop

    iTop,作为全面支持ITIL流程的一款ITSM工具,具有强大的ITSM功能,开源免费.简单易用. iTop,即IT运营门户(IT Operation Portal),是一个开源web应用程序,用于I ...

  9. UVALive 7721 K - 2-ME Set 集合dp,所有数的位或来表示状态。

    /** 题目:UVALive 7721 K - 2-ME Set 链接:https://vjudge.net/problem/UVALive-7721 题意:给定n个数,从中取出一个集合,至少包含两个 ...

  10. jsp页面form表单提交时候乱码

    1.问题描述: 表单提交中文乱码问题,怎么解决 2.原因 当表单传输到服务器上时,服务器会将传输的数据进行编码(iso-8859-1),然后当我们从服务器上面取数据的时候,就会出现乱码 3.解决的方式 ...