码迷,mamicode.com
首页 > 其他好文 > 详细

Nested Dolls (单调递增子序列 + 二分)

时间:2016-05-07 11:11:50      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

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
题目大意:

       就是有一堆的长和宽知道的玩偶,小的玩偶可以嵌套在大的里面,只有当长和宽都比它要嵌套在里面的小的时候才可以(长宽不能互换)问多有嵌套完毕后,还有多有个玩偶(最少的)。

解题思路:

       要求出最多的能嵌套的个数,如果用贪心思想,势必会TLE,必然要寻求别的方法,因为长或宽相同的不能嵌套,那么如果对长按照从大到小的顺序排列,对于长相等的按照从小到大的顺序排列,那么对宽求单调递增子序列即可。不管什么情况,都可以求得最优解。

        一个重要的是要用二分求解最长公共子序列获取最优解,主要是为了优化时间。

代码如下:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#define INF 0x3f3f3f3f
using namespace std;
struct Data
{
    int w, h;
}s[20010];

bool cmp(Data A, Data B)
{
    if(A.w != B.w)
        return A.w > B.w;
    else
        return A.h < B.h;
}

int main()
{
    int t, i, j, sum, n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i = 1; i <= n; i++)
        {
            scanf("%d%d",&s[i].w,&s[i].h);
        }
        sort(s + 1, s + n + 1, cmp); // 对长从大到小排序,宽从小到大排序
        int b[20010],dp[20010];
        memset(b, 0, sizeof(b));
        memset(dp, 0, sizeof(dp));
        sum = -1;
        b[0] = -1;
        dp[0] = 0;
        int top;
        for(i = 1, top = 0; i <= n; i++)//利用二分法对宽求最长公共子序列
        {
            if(s[i].h >= b[top])
            {
                b[++top] = s[i].h;//b数组记录最长公共子序列
                dp[i] = top;//记录当前点的最长公共子序列的元素个数

            }
            else
            {
                 int l = 1, r = top;
                 while(l <= r)//二分
                 {
                     int mid = (l + r) / 2;
                     if(s[i].h >= b[mid])
                     {
                         l = mid + 1;
                     }
                     else
                        r = mid - 1;
                 }
                 b[l] = s[i].h;
                 dp[i] = l;
            }
            if(dp[i] > sum)
               sum  = dp[i];
        }
        printf("%d\n",sum);
    }
    return 0;
}




Nested Dolls (单调递增子序列 + 二分)

标签:

原文地址:http://blog.csdn.net/bmamb/article/details/51332350

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!