Nested Dolls

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

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
思路:铺砖问题,宽度不等则按宽度有小到大排序,宽度相等则按高度由大到小排序。如果高度增加就往上摞,否则另起一堆。
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN=;
struct Node{
int w,h;
}doll[MAXN];
int n;
int dp[MAXN];
bool comp(Node no1,Node no2)
{
if(no1.w!=no2.w)
{
return no1.w < no2.w;
}
else
{
return no1.h > no2.h;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&doll[i].w,&doll[i].h);
}
sort(doll,doll+n,comp);
dp[]=doll[].h;
int k=;
for(int i=;i<n;i++)
{
int j;
for(j=;j<k;j++)
{
if(dp[j]<doll[i].h)
{
dp[j]=doll[i].h;
break;
}
}
if(j==k)
dp[k++]=doll[i].h;
}
printf("%d\n",k);
}
return ;
}
 

HDOJ1677(铺砖问题)的更多相关文章

  1. dp合集 广场铺砖问题&&硬木地板

    dp合集 广场铺砖问题&&硬木地板 很经典了吧... 前排:思想来自yali朱全民dalao的ppt百度文库免费下载 后排:STO朱全民OTZ 广场铺砖问题 有一个 W 行 H 列的广 ...

  2. 《挑战程序设计竞赛》P196 铺砖问题

    题意:给定n*m格子,每个格子被染成了黑色或者白色,现在要用1*2的砖块覆盖这些格子,块与块不得重叠,且覆盖所有的白色格子,但不覆盖任意一个黑色格子,求一共有多少种覆盖方法. 思路:书上给的思路太巧妙 ...

  3. zjnu1745 DOMINE (状压dp+1*2铺砖)

    Description Mirko has a chessboard with N rows and just three columns. Slavica has written an intege ...

  4. dp状态压缩-铺砖问题

    题目:有一个n行m列的地板,需要用 1*2小砖铺盖,小砖之间互相不能重叠,问有多少种不同的铺法? 示范: 解法:用F[i][j]存放第i行的第j状态(j为十进制,转为二进制即是状态)有多少种方案. 用 ...

  5. 铺砖问题 (状态压缩dp)

    问题描述: 给定m×n个格子,每个格子被染成了黑色或白色.现在要用1×2的砖块覆盖这些格子,要求快于快之间互相不重叠,且覆盖了所有白色的格子(用 . 表示),但不覆盖任意一个黑色的格子(用 x 表示) ...

  6. poj2411铺砖——状压DP

    题目:http://poj.org/problem?id=2411 状态压缩,一行的状态记为一个二进制数,从上往下逐行DP,答案输出最后一行填0的方案数. 代码如下: #include<iost ...

  7. [ACM] HDU 1400 Mondriaan&#39;s Dream (状态压缩,长2宽1长方形铺满)

    Mondriaan's Dream Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  8. DP专辑

    今天练了一波DP.时间紧迫我就只贴代码了. 20141120 fzu2129 http://acm.fzu.edu.cn/problem.php?pid=2129 不同的子序列个数 //#pragma ...

  9. 从一道NOI练习题说递推和递归

    一.递推: 所谓递推,简单理解就是推导数列的通项公式.先举一个简单的例子(另一个NOI练习题,但不是这次要解的问题): 楼梯有n(100 > n > 0)阶台阶,上楼时可以一步上1阶,也可 ...

随机推荐

  1. MyBaits面试题

    原文地址 Mybatis技术内幕系列博客,从原理和源码角度,介绍了其内部实现细节,无论是写的好与不好,我确实是用心写了,由于并不是介绍如何使用Mybatis的文章,所以,一些参数使用细节略掉了,我们的 ...

  2. Spark0.9.0机器学习包MLlib-Classification代码阅读

    本章主要讲述MLlib包里面的分类算法实现,目前实现的有LogisticRegression.SVM.NaiveBayes ,前两种算法针对各自的目标优化函数跟正则项,调用了Optimization模 ...

  3. MainWindows

    开发带有菜单栏状态栏等常用windows应用时候使用

  4. Linux shell join命令详解

    Linux join命令 2012-02-09 17:49:00| 分类: SHELL | 标签:linux join 文件连接 |字号 订阅join命令 功能:“将两个文件里指定栏位同样的行连接起来 ...

  5. 工作中你肯定会有关于 Yii2 的小贴士用法,在下面评论分享出来吧。

    场景: 数据库有user表有个avatar_path字段用来保存用户头像路径 需求: 头像url需要通过域名http://b.com/作为基本url 目标: 提高代码复用 此处http://b.com ...

  6. J2EE SSH框架整合教程

    本文仅作为学习和研究的参考,与实际项目使用技术有所不同,由于作者水平有限,错误疏漏在所难免,请各位看官批评指教. 项目的源代码放在:https://github.com/Frank-Pei/SSHIn ...

  7. 什么是gitlab CI ?CI代表什么?

    CI是Continuous Integration的简称,就是持续集成的意思. 就是说你代码改动了,测试了,提交了,持续集成系统会自动构建(编译等等).持续集成的理念是每个提交的版本都应该是可交付的, ...

  8. centos web+mysql服务器的安全

    今天闲来无事,拿来X-Scan-v3.3 来扫描自己的服务器,开放端口有22,80,443,3306:3306端口被扫出来,呵呵,那可不得了: 一,屏蔽扫描器扫出3306端口,因为web和数据库是在同 ...

  9. eDocEngine_3.0.4.273的手动安装

    1.安装FastReport 5: 2.编译Shared3(如对Delphi2007,打开gtSharedD11.groupproj项目文件),将产生的bpl.dcp文件分别拷贝到C:\Users\P ...

  10. eclipse中集成velocity插件

    1.打开eclipse,点击help,选择install new software 2.点击add,输入下载地址: http://download.eclipse.org/eclipse/update ...