hdu 1677 (LIS+贪心)

1、http://acm.hdu.edu.cn/showproblem.php?pid=1677

2、题目大意:现在有很多盒子,知道其长宽,小的可以放到大的里边,但是必须满足w1<w2&&h1<h2,求最终可以合并成多少个盒子

乍一看觉得贪心能做,不过数有点大,可以用LIS做,不过题目需要看出怎么能用到LIS

先按照贪心的想法,给给定的数据排序,排序方法是先按照w从大到小排序,如果w相等,则按照h从小到大排序,此方案是为了解决盒子宽相等,放不进去的,例如自己写了一个样例

4
30 10 20 13 20 14 20 15//此样例可以解释为什么选择一个升序,一个降序排列

另外要注意调用LIS模板时,此题允许数字相等,自己写了个样例

4
10 10 20 10 20 10 30 25//此样例决定了求的最长上升子序列包括相等情况

最终输出的最长上升子序列的个数就是要求的最终合并成的盒子的个数,因为排好序求出的最长上升子序列,就是最终要放到最外层的盒子,之间的盒子都可以塞到这些盒子里

3、题目:

Nested Dolls

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1714    Accepted Submission(s): 482


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
 

4、代码:

#include<stdio.h>
#include<string.h>
#define N 20005
#include<algorithm>
using namespace std;
int stack[N];
int dp[N];
int maxx;
int n;
struct node
{
    int w;
    int h;
}a[N];
int cmp(struct node b,struct node c)
{
    if(b.w==c.w)
    return b.h<c.h;
    else return b.w>c.w;
}
void LIS()
{
    //dp[i]表示前i个数字中加上第i个数字后,有几个数字是递增的,即前i个数字中有多少个比第i个数字小的+1
    //int stack[N];
    memset(dp,0,sizeof(dp));
    memset(stack,0,sizeof(stack));
    int top=0;
    stack[top]=-99999999;
    maxx=-1;
    for(int i=1; i<=n; i++)
    {
        //如果a[i]>栈顶部元素,则压栈
        if(a[i].h>=stack[top])
        {
            stack[++top]=a[i].h;
            dp[i]=top;
        }
        //如果a[i]不大于栈顶部元素,则二分查找第一个比a[i]大的元素
        else
        {
            int l=1,r=top;
            while(l<=r)
            {
                int mid=(l+r)>>1;
                if(a[i].h>=stack[mid])
                {
                    l=mid+1;
                }
                else
                    r=mid-1;
            }
            //替换a[i]
            stack[l]=a[i].h;
            dp[i]=l;
        }
        if(dp[i]>maxx)
        maxx=dp[i];
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].w,&a[i].h);
        }
        sort(a+1,a+n+1,cmp);
        LIS();
        printf("%d\n",maxx);
    }
    return 0;
}
/*
10
4
30 10 20 13 20 14 20 15//此样例可以解释为什么选择一个升序,一个降序排列
4
10 10 20 10 20 10 30 25//此样例决定了求的最长上升子序列包括相等情况
4
10 15 20 10 20 30 30 25
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
*/


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值