博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ3636Nested Dolls[DP LIS]
阅读量:4518 次
发布时间:2019-06-08

本文共 2763 字,大约阅读时间需要 9 分钟。

Nested Dolls
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 w1h1,w2h2, ... ,wmhm, where wi is the width and hi is the height of doll number i. 1 ≤ wihi ≤ 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

4320 30 40 50 30 40420 30 10 10 30 20 40 50310 30 20 20 30 10410 10 20 30 40 50 39 51

Sample Output

1232

Source

------------------------------------
貌似有一种很厉害的东西叫Dilworth定理
最少的chain个数等于最大的antichain的大小
按照w从小到大排序,剩下考虑h,chain应该是严格单增
最少几个,antichain,不就是导弹拦截的第二问,最长不下降子序列
要注意的是w相同时h大的放在前,因为w相同嵌套是违法的,WA好几次
 
////  main.cpp//  poj3636////  Created by abc on 16/8/30.//  Copyright © 2016年 abc. All rights reserved.//#include 
#include
#include
#include
using namespace std;const int N=20005,INF=1e9;struct data{ int w,h;}da[N];bool cmpda(data a,data b){ if(a.w>b.w) return 0; if(a.w
b.h?1:0; return 1;}int t,n;int f[N],g[N],a[N];bool cmp(int a,int b){ return a>b;}int dp(){ int ans=0; sort(da+1,da+1+n,cmpda); memset(f,0,sizeof(f)); for(int i=1;i<=n;i++) g[i]=-INF,a[i]=da[i].h; for(int i=1;i<=n;i++){ int k=upper_bound(g+1,g+1+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=1;i<=t;i++){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d%d",&da[i].w,&da[i].h); printf("%d\n",dp()); } return 0;}

 

 

转载于:https://www.cnblogs.com/candy99/p/5824214.html

你可能感兴趣的文章
【链表】反转
查看>>
在IIS7.5中ASP.NET调用cmd程序拒绝访问决绝方法小记
查看>>
(转载)VS2010/MFC编程入门之三十七(工具栏:工具栏的创建、停靠与使用)
查看>>
第五张标准I/O库
查看>>
SpringMVC源码阅读:属性编辑器、数据绑定
查看>>
ios iPhone的一些基础知识,扫盲
查看>>
iOS多Target管理项目
查看>>
windows下安装openssh服务并实现远程登录
查看>>
C++重载操作符
查看>>
MySQL存储过程之创建第一个存储过程
查看>>
MySQL存储过程之游标
查看>>
通过appearance设置app主题
查看>>
fis3运行项目的前准备
查看>>
async/await让我们更好的写异步代码
查看>>
react输入框输入中文bug
查看>>
Linux命令整理
查看>>
SQL2000的卸载步骤
查看>>
Sql不区分大小写查询
查看>>
二分法查找
查看>>
180. Consecutive Numbers (Medium)
查看>>