Nested Dolls (單調遞增子序列 + 二分)

來源:互聯網
上載者:User

標籤:

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

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

       就是有一堆的長和寬知道的玩偶,小的玩偶可以嵌套在大的裡面,只有當長和寬都比它要嵌套在裡面的小的時候才可以(長寬不能互換)問多有嵌套完畢後,還有多有個玩偶(最少的)。

解題思路:

       要求出最多的能嵌套的個數,如果用貪心思想,勢必會TLE,必然要尋求別的方法,因為長或寬相同的不能嵌套,那麼如果對長按照從大到小的順序排列,對於長相等的按照從小到大的順序排列,那麼對寬求單調遞增子序列即可。不管什麼情況,都可以求得最優解。

        一個重要的是要用二分求解最長公用子序列擷取最優解,主要是為了最佳化時間。

代碼如下:

#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <queue>#include <map>#include <cmath>#include <string>#define INF 0x3f3f3f3fusing 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 (單調遞增子序列 + 二分)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.