A-Nested DollsTime limit : MS
Memory Limit:32768KB
64bit IO Format:
C8>%I64D &%i64u
Description
Dilworth is the world's most prominent collector of Russian nested dolls:he literally have thousands of them! You know, the wooden hollow dolls of different sizes of which the smallest doll are contained in the second smallest, and t His doll are in turn contained in the next one and so forth. One day and he wonders if there is another the nesting them so he'll end up with fewer nested dolls? After all, this would make he 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 would fit in another doll of width W2 and height H2 if and only if W1 < W2 and H1 < ; H2. Can him calculate the smallest number of nested dolls possible to assemble from his massive list of measurements?
Input
On the first line of input are a single positive integer 1 <= t <= specifying the number of the 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 are the height of doll number I . 1 <= wi, HI <= 10000 for all I.
Output
For all test case there should is one line of output containing the minimum number of nested dolls possible.
Sample Input
Sample Output
Main topic:
There are n boxes, knowing the width and height, if the width and height of the first box is W1,H1, the second width and height is w2,h2, if W1<w2 && h1
Thinking Analysis:
This topic and the doll is a bit like the title, after sorting to H do the longest ascending subsequence, the maximum length of the output can be
The first thing you can think of is greed, constantly traversing, constantly updating. Complexity N*n (timeout!!) ), and then think of similar to the previous one, there is the idea of a binary map matching the minimum path coverage. This problem data 20,000 points, definitely timed out ...
Then began to think about the DP, first ordered, this is the key, according to the long from the big to the small sort, and then according to the high from the beginning to the big sort, at first thought that as long as all according to from big to small can be, but for the long same box this sort is wrong, very big ah, after understanding learning, Found a special cow of the train of thought, Daniel indeed cow!
The exact sort method is as follows:
according to the long pair of boxes (from large to small), and then for all boxes of a sequence of high composition, the longest monotone increment of the number of sub-sequences, this is the solution of the problem! Think about it, list the boxes that correspond to the monotonically increasing sequence, and find that any 22 of them are not nested. Then the rest of the boxes can be nested into these boxes! The idea is amazing ... For the longest monotonically incrementing subsequence, if the dynamic programming complexity n^2, or time-out .... Here again using binary search, complexity O (N*logn), on the line ...
Attached code:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include < Algorithm> #define INF 0x3f3f3f3fusing namespace std;struct a{int w,h;} Point[20010];bool CMP (A a,a B)//define sorting methods, according to W from large to small, W equal when h from small to large sort of way (H of this is very important, H of the small to arrive sort, solved when w is equal after need to use more Case of a box, since H is ordered from small to large, then the length of the longest ascending subsequence will increase) {if (A.W = = B.W) return a.h < B.h;return a.w > B.W;} int Dp[20010];int Main () {int t;cin >> t;while (t--) {int m;cin >> m;for (int i = 0;i < m;i++) {scanf ("%d%d", &am P;POINT[I].W,&POINT[I].H);} Sort (point,point + m,cmp); fill (dp,dp+m,inf); Initializes the DP array, initialized to the maximum value int ans = 0; The following is the process of finding the longest ascending subsequence, which is mentioned in the challenge programming. for (int i = 0;i < m;i++)//Using the time complexity of Nlog (n) to solve the longest ascending subsequence {*upper_bound (dp,dp+m,point[i].h) = point[i].h; Here is the use of the STL function, the principle is a binary tree, the time complexity of the same as the two points, and this can only be used UPPEr_bound () function, instead of using the Lower_bound () function as explained in the challenge programming, for what reason, the reader can figure out which value to modify when using dichotomy. }printf ("%d\n", Lower_bound (Dp,dp+m,inf)-DP); Outputs the length of the longest ascending subsequence}return 0;}
UVA 11368 & POJ 3636 & HDU 1677 Nested Dolls (greedy + two-point Lis)