POJ3636Nested Dolls[DP LIS]

Posted Candy?

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3636Nested Dolls[DP LIS]相关的知识,希望对你有一定的参考价值。

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

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

------------------------------------
貌似有一种很厉害的东西叫Dilworth定理
我就用一个普通思路吧
按照w从小到大排序,剩下考虑h,最少几个,不就是导弹拦截的第二问,最长不下降子序列
要注意的是w相同时h大的放在前,因为w相同嵌套是违法的,WA好几次
 
//
//  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=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.w) return 1;
    if(a.w==b.w) return a.h>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;
}

 

 

以上是关于POJ3636Nested Dolls[DP LIS]的主要内容,如果未能解决你的问题,请参考以下文章

POJ 1769 Minimizing maximizer(DP+zkw线段树)

POJ1821 单调队列//ST表 优化dp

POJ1821 Fence 单调队列优化DP

POJ3692 Kindergarten —— 二分图最大团

POJ1821Fence

网络流(最大流) POJ 1637 Sightseeing tour