G: Nested Dolls

Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 99     Solved: 19


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 题意:有m个嵌套娃娃,如果娃娃的高h和宽w都小于另一个娃娃,那么就能嵌套成为一个娃娃。
   问最少会剩下多少个娃娃。 很容易想到把娃娃按照宽度从大到小,高度从低到高进行排序,然后只要单独去分析高就可以找出最后有多少娃娃了; dp保存的是每次能嵌套的最后一个娃娃的高度,最后找一遍一共有多少个最后一个娃娃,就行了。
#include "cstdio"
#include "cstring"
#include "iostream"
#include "algorithm"
#include "cmath"
using namespace std;
#define memset(x,y) memset(x,y,sizeof(x)) struct box
{
int h,w;
} B[20005],dp[20005];; bool cmp(box a,box b)
{
if(a.w==b.w)return a.h<b.h;
return a.w >b.w;
}
int main()
{
int T,n;
cin>>T;
while(T--)
{
int ans=0;
cin>>n;
memset(dp,0x3f);
for(int i=0; i<n; i++)
{
scanf("%d%d",&B[i].w,&B[i].h);
}
sort(B,B+n,cmp);
int tem=dp[0].h;
for(int i=0;i<n;i++){
int j=0;
while(dp[j].h<=B[i].h){
j++;
}
dp[j].h=B[i].h;
}
for(int i=0;i<n;i++){
if(dp[i].h!=tem)ans++;
}
cout <<ans<<endl;
}
return 0;
}

  

Nested Dolls 贪心 + dp的更多相关文章

  1. hdu 1677 Nested Dolls【贪心解嵌套娃娃问题】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1677 http://acm.hust.edu.cn/vjudge/contest/view.action ...

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

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

  3. 【BZOJ-3174】拯救小矮人 贪心 + DP

    3174: [Tjoi2013]拯救小矮人 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 686  Solved: 357[Submit][Status ...

  4. BZOJ_3174_[Tjoi2013]拯救小矮人_贪心+DP

    BZOJ_3174_[Tjoi2013]拯救小矮人_贪心+DP Description 一群小矮人掉进了一个很深的陷阱里,由于太矮爬不上来,于是他们决定搭一个人梯.即:一个小矮人站在另一小矮人的 肩膀 ...

  5. 洛谷P4823 拯救小矮人 [TJOI2013] 贪心+dp

    正解:贪心+dp 解题报告: 传送门! 我以前好像碰到过这题的说,,,有可能是做过类似的题qwq? 首先考虑这种显然是dp?就f[i][j]:决策到了地i个人,跑了j个的最大高度,不断更新j的上限就得 ...

  6. 【bzoj5073】[Lydsy1710月赛]小A的咒语 后缀数组+倍增RMQ+贪心+dp

    题目描述 给出 $A$ 串和 $B$ 串,从 $A$ 串中选出至多 $x$ 个互不重合的段,使得它们按照原顺序拼接后能够得到 $B$ 串.求是否可行.多组数据. $T\le 10$ ,$|A|,|B| ...

  7. 【bzoj3174】[Tjoi2013]拯救小矮人 贪心+dp

    题目描述 一群小矮人掉进了一个很深的陷阱里,由于太矮爬不上来,于是他们决定搭一个人梯.即:一个小矮人站在另一小矮人的 肩膀上,知道最顶端的小矮人伸直胳膊可以碰到陷阱口.对于每一个小矮人,我们知道他从脚 ...

  8. hdu 1257 最少拦截系统【贪心 || DP——LIS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  9. 贪心+DP【洛谷P4823】 [TJOI2013]拯救小矮人

    P4823 [TJOI2013]拯救小矮人 题目描述 一群小矮人掉进了一个很深的陷阱里,由于太矮爬不上来,于是他们决定搭一个人梯.即:一个小矮人站在另一小矮人的 肩膀上,知道最顶端的小矮人伸直胳膊可以 ...

随机推荐

  1. 五十二、linux 编程——网络介绍

    52.1 网络介绍 使用远程资源 共享信息.程序和数据 分布处理 52.1.1 协议的概念 计算机网络中实现通信必须有一些约定,如对速率.传输代码.代码结构.传输控制步骤和出错控制等约定,这些约定即被 ...

  2. ajax向Django前后端提交请求和CSRF跨站请求伪造

    1.ajax登录示例 urls.py from django.conf.urls import url from django.contrib import admin from app01 impo ...

  3. day 14 - 2 生成器练习

    相关练习 1.处理文件,用户指定要查找的文件和内容,将文件中包含要查找内容的每一行都输出到屏幕 #比较 low 的方法 def check_file(filename,aim): with open( ...

  4. 409 javascript if and while表达式

    数组定义.特点.运算符:算术运算 ++ --(自减 自加) 赋值运算发 =比较:!= == === 逻辑运算 有 && || ! 正则表达式 修饰符 i:用来表示 g:很少演示(在第一 ...

  5. CapsNet胶囊网络(理解)

    0 - 背景 Geoffrey Hinton是深度学习的开创者之一,反向传播等神经网络经典算法发明人,他在去年年底和他的团队发表了两篇论文,介绍了一种全新的神经网络,这种网络基于一种称为胶囊(caps ...

  6. Django-1-URL路由系统

    一.分发地址 在APP中创建urls.py文件,将属于该APP的url地址都写入到这个文件中,当程序收到用户发送的请求时,先在根目录的urls.py文件中查找该地址属于哪个APP,将这个请求分发到该A ...

  7. Git入门——本地版本库操作

    作为一个一直用SVN的家伙,深深地感到了自己在版本控制工具上的落伍.... 首先必须强调的是: Git与Github不是一回事. Git是目前世界上最先进的分布式版本控制系统,于2005年被linux ...

  8. NBIOT经典回答【转】

    转自:https://blog.csdn.net/pan0755/article/details/70145936 该部分分享的是物联网各垂直应用领域里,NB-IoT技术的部署,看看适合NB-IoT技 ...

  9. 【转】mac环境下Android 反编译

    一.需要的工具 apktool:反编译APK文件,得到classes.dex文件,同时也能获取到资源文件以及布局文件. dex2jar:将反编译后的classes.dex文件转化为.jar文件. jd ...

  10. IIS 一键安装及卸载

    IIS6:适用于win server 2003:: ******************* :: * 安装 :: ******************* :Install Cls @echo. &am ...