HDU - 1677Nested Dolls最长上升子序列变式

HDU - 1677

Nested Dolls

Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

Submit Status

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 

大概意思与最小拦截系统类似

第一种方法,自己写二分函数

/*
Problem : 1677 ( Nested Dolls )     Judge Status : Accepted
RunId : 14351565    Language : G++    Author : 24862486
*/
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 20000 + 5;
const int INF=0x3f3f3f3f;
struct point {
    int h, w;
    point(int w ,int h) : w(w), h(h) {}
    point() {}
} ps[maxn];
int dp[maxn], t, m;
bool cmp(const point &a,const point &b) {
    if(a.w == b.w) return a.h < b.h;
    return a.w > b.w;
}
int solve() {
    memset(dp,0,sizeof(dp));
    int top = 0, maxx = -1;
    dp[top] = -1;
    for(int i = 1; i <= m; i ++) {
        if(ps[i].h >= dp[top]) {
            dp[++ top] = ps[i].h;//其中dp[1]--dp[top]一定是不下降子序列,因为每一次的新系统的生成,表示大于他的值
        } else {
            int l = 0,r = top;//(0,top]
            while(r - l > 1) {
                int mid = (l + r) >> 1;
                if(ps[i].h < dp[mid]) {//是不能够相等,相等则依旧取不到
                    r = mid;
                } else
                    l = mid;
            }
            dp[r] = ps[i].h;
        }
    }
    return top;
}
int main() {
    //freopen("D://imput.txt", "r", stdin);
    scanf("%d", &t);
    while(t --) {
        scanf("%d", &m);
        for(int i = 1; i <= m; i ++) {
            scanf("%d%d", &ps[i].w, &ps[i].h);
        }
        sort(ps + 1,ps + m + 1,cmp);
        printf("%d\n",solve());
    }
    return 0;
}

第二种,运用upper_bound以及lower_bound函数

/*
Problem : 1677 ( Nested Dolls )     Judge Status : Accepted
RunId : 14351713    Language : G++    Author : 24862486
*/
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 20000 + 5;
const int INF=0x3f3f3f3f;
struct point {
    int h, w;
    point(int w ,int h) : w(w), h(h) {}
    point() {}
    bool operator < (const point &object)const {
        return h < object.h;
    }
} ps[maxn],dp[maxn];
bool cmp(const point &a,const point &b) {
    if(a.w == b.w) return a.h < b.h;
    return a.w > b.w;
}
int main() {
    //freopen("D://imput.txt", "r", stdin);
    int t, m;
    scanf("%d", &t);
    while(t --) {
        scanf("%d", &m);
        for(int i = 0; i < m; i ++) {
            scanf("%d%d", &ps[i].w, &ps[i].h);
        }
        sort(ps,ps + m,cmp);
        for(int i = 0; i < m ; i ++) {
            dp[i].h = INF;
            dp[i].w = INF;
        }
        for(int i = 0; i < m ; i ++) {
            *upper_bound(dp,dp + m, ps[i]) = ps[i];//不是lower_bound()原因是不能够相等
        }
        printf("%d\n", lower_bound(dp, dp + m, point(INF,INF)) - dp);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-18 19:41:00

HDU - 1677Nested Dolls最长上升子序列变式的相关文章

hdu 1025 dp 最长上升子序列

1 //Accepted 4372 KB 140 ms 2 //dp 最长上升子序列 nlogn 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 using namespace std; 7 const int imax_n = 500005; 8 int dp[imax_n]; 9 int d[imax_n]; 10 int a[imax_n]; 11 int n; 12 int len

hdu 1160 排序 + 最长上升子序列

题意: 输出体重上升而速度下降的最长子序列 题意: 先按照结构体升序排序体重,之后用dp对速度求最长下降子序列即可. 代码: #include <set> #include <map> #include <cmath> #include <stack> #include <queue> #include <string> #include <vector> #include <cstdio> #include

【单调栈】最长不下降子序列变式

题目大意: 给定序列列 a[],最少修改多少个位置可以令其变成最长上升子序列 分析: 这道题看似非常奇怪,然而细想一下很容易发现我们可以通过令 a'[i] = a[i] - i 对 a'[i] 求最长上升子序列,可以得到最多有多少个位置保持不不变 然后用n去减它,就是要修改的个数 代码如下: #include<cstdio> #include<iostream> #include<algorithm> using namespace std; int n,top=1,h

!HDU 1513 Palindrome--dp--(最长公共子序列模型)

题意:给定一个字符序列,求最少添加多少个字符能让它变成对称序列 分析:这题的做法竟然是把序列颠倒之后求最长公共子序列,然后n-dp[n][n]就是答案.记住这种做法. 在这里再说一次最长公共子序列的做法:dp[i][j]表示序列1的前i个字符和序列2的前j个字符比较时的最长公共子序列的长度,状态转移公式:1.当a[i]==b[j]时,dp[i][j]=dp[i-1][j-1]+1:2.否则,dp[i][j]=max(dp[i-1][j],dp[i][j-1]) 代码: #include<iost

hdu 5748(求解最长上升子序列的两种O(nlogn)姿势)

Bellovin Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 707 Accepted Submission(s): 328 Problem Description Peter has a sequence a1,a2,...,an and he define a function on the sequence -- F(a1,a2

Bridging signals hdu 1950 (最长上升子序列)

http://acm.split.hdu.edu.cn/showproblem.php?pid=1950 题意:求最长上升(不连续or连续)子序列 推荐博客链接: http://blog.csdn.net/sinat_30062549/article/details/47197073 #include <iostream> #include <stdio.h> #include <string.h> #include <string> #include &l

HDU 1069 dp最长递增子序列

B - Monkey and Banana Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1069 Appoint description: Description A group of researchers are designing an experiment to test the IQ of a monkey. They wi

HDU 1159 LCS最长公共子序列

1 #include <cstdio> 2 #include <cstring> 3 4 using namespace std; 5 const int N = 1005; 6 #define max(a,b) a>b?a:b 7 8 char a[N] , b[N]; 9 int dp[N][N]; 10 11 int main() 12 { 13 while(scanf("%s%s" , a+1 , b+1) != EOF){ 14 int l1 =

最长上升子序列 和 最长不下降子序列

最长上升子序列:是严格上升的,a<b<c<d这样的,即为严格单调递增,用lower_bound lower_bound:返回序列中大于等于key值的第一个数 比如说序列 1 2 3 4 5,key值为3,使用lower_bound返回第三个数 最长不下降子序列:不是严格上升,可以存在多个数相等的情况,用upper_bound upper_bound:返回序列中严格大于key值的第一个数 比如说序列1 2 3 4 5,key值为3,使用upper_bound返回第四个数 Hdu 1950