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:
is a bunch of long and wide-known dolls, small dolls can be nested in the big inside, only when the length and width of the inside of the smaller than it is to nest in the small time can (the length of the width is not interchangeable) asked more nested after the completion, there is more than a doll (the least).
Problem Solving Ideas:
Ask the number of the most can be nested, if greedy thought, will be tle, must seek other methods, because long or wide the same can not be nested, then if the long according to the order from large to small, for long equal in order from small to large, then the width of the monotone increment sub-sequence can be. Whatever the situation, the optimal solution can be obtained.
One important thing is to get the optimal solution for the longest common sub-sequence with two points, mainly to optimize the time.
The code is as follows:
#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); For long from big to small sort, wide from small to large sort 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++)//Use dichotomy to find the longest common subsequence {if (s[i].h >= b[top]) { B[++top] = s[i].h;//b Array records the longest common subsequence dp[i] = top;//records the current point of the mostThe number of elements of the long common subsequence} else {int L = 1, r = top; while (l <= r)//dichotomy {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 (monotonically incrementing sub-sequence + dichotomy)