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

Problem Description
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 ?

 
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 ith integer Ai(0<Ai<1000000000 ) means that the ith 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
 
时间复杂度:
代码:

#include <bits/stdc++.h>
using namespace std; int L, n;
int len[610]; bool cmp(int x, int y) {
return x > y;
} int main() {
while(~scanf("%d%d", &L, &n)) {
int sum = 0, cnt = 0;
for(int i = 1; i <= n; i ++)
scanf("%d", &len[i]); sort(len + 1, len + 1 + n, cmp);
while(sum < L && cnt <= n) {
sum += len[cnt + 1];
cnt ++;
}
if(cnt <= n)
printf("%d\n", cnt);
else
printf("impossible\n");
}
return 0;
}

  

HDU 2124 Repair the Wall的更多相关文章

  1. --hdu 2124 Repair the Wall(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2124 Ac code : #include<stdio.h> #include<st ...

  2. 杭电 2124 Repair the Wall(贪心)

    Description Long time ago , Kitty lived in a small village. The air was fresh and the scenery was ve ...

  3. 简单贪心) Repair the Wall hdu2124

    Repair the Wall http://acm.hdu.edu.cn/showproblem.php?pid=2124 Time Limit: 5000/1000 MS (Java/Others ...

  4. HDU2124 Repair the Wall(贪心)

    Problem Description Long time ago , Kitty lived in a small village. The air was fresh and the scener ...

  5. hdu 3669 Cross the Wall(斜率优化DP)

    题目连接:hdu 3669 Cross the Wall 题意: 现在有一面无限大的墙,现在有n个人,每个人都能看成一个矩形,宽是w,高是h,现在这n个人要通过这面墙,现在只能让你挖k个洞,每个洞不能 ...

  6. Repair the Wall

    问题 : Repair the Wall 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Long time ago , Kitty lived in a small village. ...

  7. Repair the Wall (贪心)

    Long time ago , Kitty lived in a small village. The air was fresh and the scenery was very beautiful ...

  8. hdu 1543 Paint the Wall

    http://acm.hdu.edu.cn/showproblem.php?pid=1543 #include <cstdio> #include <cstring> #inc ...

  9. HDU 4391 Paint The Wall(分块+延迟标记)

    Paint The Wall Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. ruby中的respond to ?用法

    今天写脚本,遇到了这个函数,遂搜索及阅读相关代码,整理如下: respond_to 是判断是否是某个类型的方法,比如: ar = "ss" p ar.respond_to?(:to ...

  2. Qt udp 主机和虚拟机无法互相广播

    描述: 主机和虚拟机可以ping通,port没被占用,虚拟机可以向主机广播,但是主机不能向虚拟机广播 原因: 虚拟机只配置了一个适配器,而主机有多个适配器,当虚拟机广播时,只能使用和主机连接的适配器, ...

  3. BAT-运行程序

    @echo offrem copy C:\Users\Administrator\Desktop\0000\123.txt C:\Users\Administrator\Desktop\0000\45 ...

  4. 24-集成ASP.NETCore Identity采用EF

    1-增加IdentityServer4.AspNetIdentity nuget包 2- StartUp.cs启用增加相应的代码  .AddAspNetIdentity<ApplicationU ...

  5. 10-第三方ClientCredential模式调用

    1-新建.net core控制台应用程序 E:\coding\netcore\IdentityServerSample>dotnet new console --name ThridPartDe ...

  6. Java设计模式(9)——结构型模式之装饰模式(Decorator)

    一.概述 动态地给一个对象添加一些额外的职责.就增加功能来说, Decorator模式相比生成子类更为灵活.该模式以对客 户端透明的方式扩展对象的功能. UML简图 角色 在持有Component的引 ...

  7. 西安Uber优步司机奖励政策(12月14日到12月20日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  8. Shr-前端汇总

    F7控件监听 f7控件的监听是指,在点击F7控件时,对控件内的内容进行选中后出发该事件监听:通过参数value可以获取当前F7控件的一些值信息. //人力编制方案监听 回写计划期间 及分录数据 ini ...

  9. LeetCode: 57. Insert Interval(Hard)

    1. 原题链接 https://leetcode.com/problems/insert-interval/description/ 2. 题目要求 该题与上一题的区别在于,插入一个新的interva ...

  10. java 编码二进制写法、十六进制用源代码表示

    二进制: int a = 0b10; a其实=2 八进制: int a = 01; a其实=8 十六进制: int a = 0x1; a其实=16