[UVA][dp、BIT] 11368 - Nested Dolls@Morris' Blog|PChome Online 人新台
2013-05-07 10:54:36| 人2,706| 回0 | 上一篇 | 下一篇

[UVA][dp、BIT] 11368 - Nested Dolls

0 收藏 0 0 站台


  G: Nested Dolls 

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?

epsfbox{p11368.eps}

Input 

On the first line of input is a single positive integer 1$ le$t$ le$20 specifying the number of test cases to follow. Each test case begins with a positive integer 1$ le$m$ le$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$ le$wi, hi$ le$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 



被於 LIS,但目求的是最少的 LIS 覆所有列元素。
但事上是很好的,反求 LDS, LDS 就是答案。
那就可以在 O(nlogn) 之求解。

特另一解法,最少路覆,可以成最小路覆的 DAG ,
但效率 O(n^3),然上不了。

其有,因算 LDS 的件是 !(
w1 < w2 and h1 < h2)
那反就是
w1 >= w2 or h1 >= h2 , 藉由 dp 的概念後,因是 "or",。
因此需要一查找
[w2 , max_range] 元素中的最大值何?同理 h

因此使用 Binary Indexed Tree 去做。

由於序的, w 做升排序, h 做次降排序。


#include <stdio.h>
#include <algorithm>
using namespace std;
struct Rect {
    int h, w;
    bool operator<(const Rect &A) const {
        if(A.h != h)
            return h < A.h;
        return w > A.w;
    }
};
Rect D[20000];
int query(int idx, int arr[]) {
    int ret = 0;
    while(idx) {
        ret = max(ret, arr[idx]);
        idx -= idx&(-idx);
    }
    return ret;
}
void modify(int idx, int arr[], int L, int argv) {
    while(idx <= L) {
        arr[idx] = max(arr[idx], argv);
        idx += idx&(-idx);
    }
}
int main() {
    int testcase;
    int n, i, j;
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d", &n);
        for(i = 0; i < n; i++) {
            scanf("%d %d", &D[i].h, &D[i].w);
        }
        sort(D, D+n);
        int dp[20005] = {};
        int wbit[10005] = {}, hbit[10005] = {};
        int ret = 0;
        for(i = 0; i < n; i++) {
            int f1 = query(10005-D[i].h, hbit);
            int f2 = query(10005-D[i].w, wbit);
            dp[i] = max(dp[i], f1+1);
            dp[i] = max(dp[i], f2+1);
            modify(10005-D[i].h, hbit, 10005, dp[i]);
            modify(10005-D[i].w, wbit, 10005, dp[i]);
            ret = max(ret, dp[i]);
        }
        printf("%d\n", ret);
    }
    return 0;
}
/*
1000
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
4
1 3 9 6 10 7 10 5
3
9 6 10 7 10 5
2
10 10 10 10

Output
1
2
3
2
2
2
2
*/

台: Morris
人(2,706) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][dijkstra] 11367 - Full Tank?
此分上一篇:[UVA][最大平均值+二分答案] 1392 - DNA Regions

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86