首页 > ACM题库 > HDU-杭电 > hdu 2124 Repair the Wall-贪心-[解题报告]C++
2013
12-29

hdu 2124 Repair the Wall-贪心-[解题报告]C++

Repair the Wall

问题描述 :

Long time ago , Kitty lived in a small village. The air was fresh and the scenery was very beautiful. The only thing that troubled her is the typhoon.

When the typhoon came, everything is terrible. It kept blowing and raining for a long time. And what made the situation worse was that all of Kitty’s walls were made of wood.

One day, Kitty found that there was a crack in the wall. The shape of the crack is
a rectangle with the size of 1×L (in inch). Luckly Kitty got N blocks and a saw(锯子) from her neighbors.
The shape of the blocks were rectangle too, and the width of all blocks were 1 inch. So, with the help of saw, Kitty could cut down some of the blocks(of course she could use it directly without cutting) and put them in the crack, and the wall may be repaired perfectly, without any gap.

Now, Kitty knew the size of each blocks, and wanted to use as fewer as possible of the blocks to repair the wall, could you help her ?

输入:

The problem contains many test cases, please process to the end of file( EOF ).
Each test case contains two lines.
In the first line, there are two integers L(0<L<1000000000) and N(0<=N<600) which
mentioned above.
In the second line, there are N positive integers. The ith integer Ai(0<Ai<1000000000 ) means that the ith block has the size of 1×Ai (in inch).

输出:

The problem contains many test cases, please process to the end of file( EOF ).
Each test case contains two lines.
In the first line, there are two integers L(0<L<1000000000) and N(0<=N<600) which
mentioned above.
In the second line, there are N positive integers. The ith integer Ai(0<Ai<1000000000 ) means that the ith block has the size of 1×Ai (in inch).

样例输入:

5 3
3 2 1
5 2
2 1

样例输出:

2
impossible

2011-12-16 06:40:40

地址:http://acm.hdu.edu.cn/showproblem.php?pid=2124

题意:用n个长度各不相同的方块堵住长度为L的墙。可以锯,问最少需要多少块。

mark:简单题,贪心。排序即可。

wa了几百次!!!搞了40分钟!!!尼玛没考虑到impossible的时候数组越界啊我日!!!太2了。条件i < n+1 没写。

其实还有一个bug,就是n==0的情况。不过ac了,就不鸟它了。

网上说要用long long,其实int都可以过的。。。

# include <stdio.h>
# include <stdlib.h>

int a[610] ;
int cmp(const void *a, const void *b)
{
    return *(int*)b - *(int*)a ;
}


int main ()
{
    int i, L, n ;
    while (~scanf ("%d%d", &L, &n))
    {
        for (i = 1 ; i <= n ; i++)    
            scanf ("%d", a+i) ;
        qsort(a+1, n, 4, cmp) ;
        for (i = 1 ; i <= n ;i++)
        {
            a[i] += a[i-1] ;
            if (a[i] >= L)
                break ;
        }
        if (i < n+1 && a[i] >=L) printf ("%d\n", i) ;
        else puts ("impossible") ;
    }
}

解题转自:http://www.cnblogs.com/lzsz1212/archive/2012/01/06/2314952.html


  1. 去搜地球史上最震撼的访谈,大卫艾克采访的非洲萨满科瑞多.姆特瓦。他在10几年前就开始揭露蜥蜴人和光明会的阴谋了。 难道你没听说过他?

  2. 博主您好,这是一个内容十分优秀的博客,而且界面也非常漂亮。但是为什么博客的响应速度这么慢,虽然博客的主机在国外,但是我开启VPN还是经常响应很久,再者打开某些页面经常会出现数据库连接出错的提示

  3. 第23行:
    hash = -1是否应该改成hash[s ] = -1

    因为是要把从字符串s的start位到当前位在hash中重置

    修改提交后能accept,但是不修改居然也能accept