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. springboot工程自动生成工具

    1 springboot工程自动生成网址 http://start.spring.io/ 2 工具 Spring Boot CLI

  2. iOS解决导航引起视图高度问题

    经过导航栏跨越的坑,总结出有两种方法可以无痕解决(前提>=iOS7版本)(TabBar与导航栏类似) 1.通过设置导航栏的透明度实现(这种方式的控制器view的起始坐标是充(0,64)开始的) ...

  3. Web Service概念辨析

    Web Service包含两个概念. 其一是Web Service标准体系,由SOAP.WSDL.UDDI三要素组成,是平台和语言无关的.在这个概念里和WCF做比较是错误的,因为前者是行业标准,后者是 ...

  4. JS中如何获取<Select>中value和text的值

    原文地址:JS中如何获取<Select>中value和text的值 html代码: <select id = "city" onchange="chan ...

  5. android客户端登录&注册的实现

    MainActivity多线程的实现: package com.example.loginconnect; import java.lang.ref.WeakReference; import jav ...

  6. java中接口的概念及使用(补充final修饰符的使用)

    接口 初期理解,可以是一个特殊的抽象类 当抽象类中的方法都是抽象的,那么该类可以通过接口的形式来表示 class 用于定义类 interface 用于定义接口 接口定义时,格式特点: 1.接口中常见的 ...

  7. 【leetcode刷题笔记】Linked List Cycle

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

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

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

  9. wget 实现web监控脚本

    #!/bin/sbin timeout= times= url=https://1.1.1.1 while true;do wget --no-check-certificate --timeout= ...

  10. Linux Shell文件差集

    file1-file2 => file3file1=/data/aaafile2=/data/bbbfile3=/data/cccsort -m <(sort $file1 | uniq) ...