POJ3636Nested Dolls[DP LIS]
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 8323 | Accepted: 2262 |
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 h= 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
Source
//
// main.cpp
// poj3636
//
// Created by abc on 16/8/30.
// Copyright © 2016年 abc. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=,INF=1e9;
struct data{
int w,h;
}da[N];
bool cmpda(data a,data b){
if(a.w>b.w) return ;
if(a.w<b.w) return ;
if(a.w==b.w) return a.h>b.h?:;
return ;
}
int t,n;
int f[N],g[N],a[N];
bool cmp(int a,int b){
return a>b;
}
int dp(){
int ans=;
sort(da+,da++n,cmpda);
memset(f,,sizeof(f));
for(int i=;i<=n;i++) g[i]=-INF,a[i]=da[i].h;
for(int i=;i<=n;i++){
int k=upper_bound(g+,g++n,a[i],cmp)-g;
f[i]=k;
g[k]=a[i];
ans=max(ans,f[i]);
}
return ans;
} int main(int argc, const char * argv[]) {
scanf("%d",&t);
for(int i=;i<=t;i++){
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d%d",&da[i].w,&da[i].h);
printf("%d\n",dp());
}
return ;
}
POJ3636Nested Dolls[DP LIS]的更多相关文章
- hdu----(1677)Nested Dolls(DP/LIS(二维))
Nested Dolls Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 洛谷P1108 低价购买[DP | LIS方案数]
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
- hdu----(1257)最少拦截系统(dp/LIS)
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- hdu--(1025)Constructing Roads In JGShining's Kingdom(dp/LIS+二分)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- hdu 4352 "XHXJ's LIS"(数位DP+状压DP+LIS)
传送门 参考博文: [1]:http://www.voidcn.com/article/p-ehojgauy-ot.html 题解: 将数字num字符串化: 求[L,R]区间最长上升子序列长度为 K ...
- BZOJ.1109.[POI2007]堆积木Klo(DP LIS)
BZOJ 二维\(DP\)显然.尝试换成一维,令\(f[i]\)表示,强制把\(i\)放到\(a_i\)位置去,现在能匹配的最多数目. 那么\(f[i]=\max\{f[j]\}+1\),其中\(j& ...
- HDU 4352 XHXJ's LIS 数位dp lis
目录 题目链接 题解 代码 题目链接 HDU 4352 XHXJ's LIS 题解 对于lis求的过程 对一个数列,都可以用nlogn的方法来的到它的一个可行lis 对这个logn的方法求解lis时用 ...
- Codeforces.264E.Roadside Trees(线段树 DP LIS)
题目链接 \(Description\) \(Solution\) 还是看代码好理解吧. 为了方便,我们将x坐标左右反转,再将所有高度取反,这样依然是维护从左到右的LIS,但是每次是在右边删除元素. ...
- HDU 4352 - XHXJ's LIS - [数位DP][LIS问题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4352 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
随机推荐
- 用js动态生成css代码
有时候我们需要利用js来动态生成页面上style标签中的css代码,方法很直接,就是直接创建一个style元素,然后设置style元素里面的css代码,最后把它插入到head元素中.但有些兼容性问题我 ...
- AH00098 pid file overwritten
错误日志: 由于定义了: <IfModule mpm_winnt_module> ThreadsPerChild 450 MaxConnectionsPerChild 4000 Accep ...
- 怎么把多个GridView和Repeater导入到word或者excel中
最近开发网上商城有发现我们会涉及到订单打印的问题,本来想到直接显示打印即可但是考虑还得下载到本地,最后只有直接选择控件按流方式输出. 导入到word中代码如下: protected void Butt ...
- Web Audio API_基本概念
Audio Context 音频的工作环境.类比做化学实验,Audio Context 就是为我们提供各种仪器和材料的实验室(严格地来说制造这些仪器材料的方法和工具也一并提供了).通常来说做实验一间实 ...
- [ javascript canvas 插件演示 ] canvas 插件演示
<!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...
- 造成OOM(内存溢出)的几种情况
数据库Cursor没关.当我们操作完数据库后,一定要调用close()释放资源. 构造Adapter没有使用缓存ContentView. @Override public View getView(i ...
- iOS---检测系统通知开关状态
if (iOS8) { //iOS8以上包含iOS8 if ([[UIApplication sharedApplication] currentUserNotificationSettings].t ...
- App.Config 和 WebConfig 特殊字符的转义码对应关系
Web.Config默认编码格式为UTF-8,对于XML文件,要用到实体转义码来替换.对应关系如下: 字符 转义码 & 符号 & & 单引号 ' ' 双引号 ...
- 0015 Java学习笔记-集合-TreeMap集合
主要的方法 构造方法: TreeMap(); TreeMap(Comparator<?super K> comparator); TreeMap(Map<? extends K,? ...
- HDFS
1.HDFS shell 1.0查看帮助 hadoop fs -help <cmd> 1.1上传 hadoop fs -put <linux上文件> <hdfs上的路径& ...