您的位置:首页 > 其它

hdu 1677 Nested Dolls LIS + 动态规划

2017-04-14 16:11 344 查看
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

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

Source

2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)

题意:

      给你 N 个娃娃, 每个娃娃有特定的 w 和 h

      当且仅当 d1.w < d2.w && d1.h < d2.h , d1 才可以放入 d2中

      问:最少还可以剩下几个娃娃

算法:应该是要用 dp 做了,这里用贪心Orz

          最后发现都一样,贪心还是 DP , 分析了 hdu 1257 就知道了Orz

思路:

      感觉类似于hdu 1257 最少拦截系统  的非DP解法【贪心求解】 hdu
1257 题解

      先对娃娃们排序:先按照 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

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
int w, h;
}p[200001];
int dp[200001]; ///存储排序结果
bool cmp(node x, node y)
{
if(x.w != y.w)
return x.w > y.w;
else if(x.w == y.w)
return x.h < y.h;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i = 0; i < n; i++)
{
scanf("%d%d",&p[i].w, &p[i].h);
}
sort(p, p + n, cmp);
int sum = 0;
memset(dp, 0, sizeof(dp));
for(int i = 0; i < n; i++)
{
int l = 0;
int r = sum;
while(l < r)  ///利用二分查找可以达到的是较复杂度为nlogn
{
int mid = (l + r) / 2;
if(dp[mid] <= p[i].h)
l = mid + 1;
else
r = mid;
}
dp[l] = p[i].h; ///dp数组里面存的就是所能嵌套的最小的娃娃
if(l == sum)  ///如果下一个娃娃都不能嵌套在之前所有的娃娃内  那么把这个娃娃情况再数组存下来  个数加一
sum++;
}
printf("%d\n",sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: