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;
}

  

最新文章

  1. 【Python】引用计数
  2. Javascript基础系列之(六)循环语句(for循环)
  3. Unity-Animator深入系列---fullPathHash和shortNameHash
  4. asp.net web api [FromBody]参数
  5. Linux中文件描述符fd和文件指针flip的理解
  6. vs2008+cmake2.8+OpenCV2.8.4配置过程中OpenCV.sln重编译部分工程失败
  7. Azure Site Recovery 通过一键式流程将虚拟机故障转移至 Azure虚拟机
  8. 设计模式——工厂模式 (C++实现)
  9. java第二次实验
  10. iOS-UI控件优化
  11. PLSQL创建Oracle定时任务
  12. excel 用VBA将所有单元格内容全部转换为文本
  13. Ubuntu系统启动后停在(initramfs)
  14. chrome如何添加扩展程序及登录
  15. crm 理解
  16. C#6.0 语法
  17. tqdm:Python 进度条
  18. Knight Tournament 合并区间
  19. iterm2 恢复默认设置
  20. 使用&lt;% =Type%&gt;获取后台值时报错:控件包含代码块(即 &lt;% ... %&gt;),因此无法修改控件集合。

热门文章

  1. Jenkins-在windows上安装及其部署
  2. python脚本 用sqoop把mysql数据导入hive
  3. LINQ to SQL 的常见异常及解决办法
  4. sqlserver 生成脚本执行创建索引
  5. Contest2161 - 2019-3-21 高一noip基础知识点 测试4 题解版
  6. python常用校验方法总结
  7. Vue导出json数据到Excel表格
  8. 题解 P5065 【[Ynoi2014]不归之人与望眼欲穿的人们】
  9. MinGW GCC 8.1.0 2018年5月2日 出炉啦
  10. Python-Django 模型层-多表查询-2