Nested dolls
Time Limit: 3000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2704 accepted submission (s): 802
Problem descriptiondilworth 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 wocould 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?
Inputon 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 2 m 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.
Outputfor each test case there shocould be one line of output containing the minimum number of nested dolls possible.
Sample input4320 30 40 50 30 40420 30 10 10 30 20 40 50310 30 20 20 30 10410 10 30 40 50 39 51
Sample output1232
Source 2008 "insigma International Cup" Zhejiang Collegiate Programming Contest-warm up (3) Question: give you n dolls, each of which has a certain height and width, then the inside of the doll is hollow. Now you need to put the little doll in the belly of the big doll, and then ask the last few dolls .. it's a bit like a box, processing method. Using dplis two-dimensional processing: A property such as height is now used as the standard to sort from large to small. When an equal width is met, the other property H is sorted from small to large. Code:
1 // # define local 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <iostream> 6 using namespace STD; 7 const int maxn = 20005; 8 const int INF = 0x3f3f3f3f; 9 int m; 10 11 struct doll12 {13 int W, H; 14 bool operator <(const doll &) const15 {16 if (W =. w) 17 return H <. h; // é ý ð ò 18 else19 return W>. w; // 109µ0000000020} 21}; 22 23 24 doll AA [maxn], ANS [maxn]; 25 int DP [maxn]; 26 27 int binary (doll v) 28 {29 int L = 1, R = m, mid; 30 While (L <= r) 31 {32 mid = L + (R-l)> 1); // descending order 33 If (ANS [Mid]. h <= v. h) 34 L = Mid + 1; 35 else36 r = mid-1; 37} 38 return l; 39} 40 41 int Lis (doll a [], int N) 42 {43 int I; 44 int res = 0; 45 for (I = 1; I <= N; I ++) 46 {47 ans [I]. H = inf; 48 ans [I]. W = inf; 49} 50 for (I = 1; I <= N; I ++) {51 DP [I] = binary (A [I]); 52 If (RES <DP [I]) RES = DP [I]; 53 If (ANS [DP [I]. h> A [I]. H & Ans [DP [I]. w> A [I]. w) {54 ans [DP [I]. H = A [I]. h; 55 ans [DP [I]. W = a [I]. w; 56} 57} 58 return res; 59} 60 61 int main () 62 {63 # ifdef local64 freopen ("test. in "," r ", stdin); 65 # endif66 67 int CAS; 68 scanf (" % d ", & CAS); 69 while (CAS --) {70 scanf ("% d", & M); 71 for (INT I = 1; I <= m; I ++) {72 scanf ("% d", & aa [I]. w, & aa [I]. h); 73} 74 sort (AA + 1, AA + m + 1); 75 printf ("% d \ n", Lis (AA, m )); 76} 77 return 0; 78}
View code
HDU ---- (1677) nested dolls (DP/LIS (2D ))