Repair the Wall(简单的贪心)

题目链接:https://vjudge.net/contest/279984#problem/E
题目大意:就是有个1L的矩形缝隙,然后有n个1a[i]的小木块来填补空隙,这些小木块可以锯,问最少要用多少块小木块才能修补好缝隙。
思路:就是贪心的选取长的木块,直到木块的长度大于等于L为止。

#include <stdio.h>
#include <algorithm>

using namespace std;

int main()
{
    int N,L;
    int a[605];
    while(~scanf("%d%d",&L,&N))
    {
        for(int i=0;i<N;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+N,greater<int>());
        int sum=0;
        int ans;
        for(int i=0;i<N;i++)
        {
            sum+=a[i];
            if(sum>=L)
            {
                ans=i+1;
                break;
            }
        }
        if(sum>=L)
            printf("%d\n",ans);
        else
            printf("impossible\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44049850/article/details/86650485
今日推荐