Nested DollsTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1876 Accepted Submission(s): 524
Problem 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
Sample Output
Source
Recommend
lcy
|
|
最後發現都同樣,貪心仍是 DP , 分析了 hdu 1257 就知道了Orzphp
/***************************************************************************************** E Accepted 540 KB 343 ms C++ 1172 B 2013-08-04 21:16:03 題意:給你 N 個娃娃, 每一個娃娃有特定的 w 和 h 當且僅當 d1.w < d2.w && d1.h < d2.h , d1 才能夠放入 d2中 問:最少還能夠剩下幾個娃娃 算法:應該是要用 dp 作了,這裏用貪心Orz 思路:感受相似於導彈攔截系統 先對娃娃們排序:先按照 w 從大到小排序, w 相同,則按照 h 從小到大排序 關於排序:先按照 w 從大到小排序能夠理解吧【把小的嵌套到大的當中去】 對於 w 相同時 h 從小到大排序 若是 m 個 w 相同, 那麼必然是嵌套在 m 個不一樣的娃娃中 【多是比它大的,也多是它自己】 先選擇 h 最小的嵌入到前面可以知足條件的娃娃中, 再新用一個娃娃嵌套 h 倒數第二小的娃娃, 那麼這時嵌套了第二個娃娃的東西,必定能比已經嵌套了第個一的更能嵌套其它的娃娃 w 相同 ,而 h 更優【h大】 好比說這堆娃娃有這樣幾個娃娃 5 30 400 (1) 10 200 (1) 10 300 (2) w 相同,必然從新嵌入不一樣的娃娃 9 250 (2) 8 250 (3) 那麼最優的結果就是 3, 第一個娃娃嵌套第二個; 第三個娃娃嵌套第四個; 第五個娃娃單獨嵌套。 可是若是你按照 w 相同時 h 從大到小排序就是這樣 5 30 400 (1) 10 300 (1) 10 200 (2) 9 250 (3) 8 250 (4) 同樣的數據, 答案是 4 *****************************************************************************************/ #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; const int maxn = 20000+10; const int INF = 10000+10; struct Node { int w,h; }node[maxn], dp[maxn]; bool cmp(Node a, Node b) { if(a.w == b.w) return a.h < b.h; else return a.w > b.w; } int main() { int T; int n; scanf("%d", &T); while(T--) { scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d%d", &node[i].w, &node[i].h); dp[i].w = dp[i].h = INF; //假設初始化有 N 個能夠嵌套的娃娃 } sort(node, node+n, cmp); //for(int i = 0; i < n; i++) printf("%d %d\n", node[i].w, node[i].h); printf("\n"); for(int i = 0; i < n; i++) { int j = 0; while(dp[j].w <= node[i].w || dp[j].h <= node[i].h) j++; dp[j].w = node[i].w; //不斷更新爲當前狀況 dp[j].h = node[i].h; } // for(int i = 0; i < n; i++) printf("%d %d\n", dp[i].w, dp[i].h); printf("\n"); int ans = 0; for(int i = 0; i < n; i++) if(dp[i].h != INF) //看有幾個嵌套過 ans++; printf("%d\n", ans); } return 0; }