HDUOJ贪心算法Repair the wall

Input

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 i th integer Ai(0<Ai<1000000000 ) means that the i th block has the size of 1×Ai (in inch).

Output

For each test case , print an integer which represents the minimal number of blocks are needed.
If Kitty could not repair the wall, just print "impossible" instead.

Sample Input

5 3
3 2 1
5 2
2 1

Sample Output

2
impossible
问题描述:
多次输入,分两行,第一行有两个数,第一个是需要的长度,第二个是现有木材的个数,第二行是现有木材的长度,如果现有木材满足需要输出最少的木材数目,否则输出impossible

#include <iostream> #include<algorithm> using namespace std; bool cmp(int a,int b) {     return a>b; } int main() {     int L,a[601],n,i,sum,num;     while(cin>>L>>n)     {         for(i=0; i<n; i++)             cin>>a[i];         sort(a,a+n,cmp);         num=sum=0;         for(i=0; i<n; i++)             if(sum<L)             {                 sum+=a[i];                 num++;             }             else                 break;         if(sum<L)             cout<<"impossible"<<endl;

        else             cout<<num<<endl;

    }     return 0; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值