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. 我的Android进阶之旅------>WindowManager.LayoutParams介绍

    本文转载于: http://hubingforever.blog.163.com/blog/static/171040579201175111031938/ 本文参照自: http://develop ...

  2. python+NLTK 自然语言学习处理七:N-gram标注

    在上一章中介绍了用pos_tag进行词性标注.这一章将要介绍专门的标注器. 首先来看一元标注器,一元标注器利用一种简单的统计算法,对每个标识符分配最有可能的标记,建立一元标注器的技术称为训练. fro ...

  3. wechat JS-SKD (getLoaction) 定位显示百度map

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  4. Android Resources

    Ref:Android开发最佳实践 Ref:Android高手速成--第一部分 个性化控件(View) Ref:Android高手速成--第二部分 工具库 Ref:Android高手速成--第三部分 ...

  5. Django与Vue语法冲突问题完美解决方法

    当我们在django web框架中,使用vue的时候,会遇到语法冲突. 因为vue使用{{}},而django也使用{{}},因此会冲突. 解决办法1: 在django1.5以后,加入了标签: {% ...

  6. 免费好用的Diff和Merge工具大总结

    总结:比较下来:diffmerge和P4merge最好用,kdiff比较专业些,支持自动merge. 一 csdiff 下载:http://www.componentsoftware.com/Prod ...

  7. Gateway

    网关在传输层上以实现网络互连,是最复杂的网络互连设备,仅用于两个高层协议不同的网络互连.网关的结构也和路由器类似,不同的是互连层.网关既可以用于广域网互连,也可以用于局域网互连. 网关是一种充当转换重 ...

  8. Ubuntu15.10下***搭建及GUI客户端安装

    1.依赖包安装 sudo apt-get install python-pip python-dev build-essential sudo pip install pip sudo apt-get ...

  9. Understanding Linux File Permissions

    Although there are already a lot of good security features built into Linux-based systems, one very ...

  10. 算法(Algorithms)第4版 练习 1.3.37

    package com.qiusongde.creative; import com.qiusongde.Queue; import edu.princeton.cs.algs4.StdOut; pu ...