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. C语言之二叉树

    规定:根节点的值大于左节点但小于右节点的值,所以二叉树的值插入是唯一的,最后形成的树只跟根节点有关 定义节点: struct tree_node {      TypeElem  elem; stru ...

  2. ES基础知识与高频考点梳理

    知识点梳理目录列表 变量类型 JS的数据类型分类和判断 值类型和引用类型 原型与原型链(继承) 原型和原型链的定义 继承写法 作用域和闭包 执行上下文 this 闭包是什么 异步 同步VS异步 异步和 ...

  3. 爬取 StackOverFlow 上有关于 Python 的问题

    给定起始页面以及爬取页数,要求得到每一个问题的标题.票数.回答数.查看数 stackflow <- function(page){ url <- "http://stackove ...

  4. Java技术——Iterator和Enumeration的不同

    个函数接口.Iterator除了能读取集合的数据之外,也能对数据进行删除操作.尽管前者还没有被弃用但是已经被后者所代替了,Enumeration已经过时的,之所以没有被弃用是因为它仍被几种从以前版本遗 ...

  5. 优步北京B组奖励政策

    用户组:优步北京B组(2015年7月20日前激活的部分司机) 更新日期:2015年8月4日 滴滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最 ...

  6. COGS 2199. [HZOI 2016] 活动投票

    2199. [HZOI 2016] 活动投票 ★★   输入文件:hztp.in   输出文件:hztp.out   简单对比时间限制:0.5 s   内存限制:2 MB [题目描述] 衡中活动很多, ...

  7. Java:List判空的条件:List=null 和 List.size = 0

    当需要对一个LIst进行判空操作时我们可使用如下两个语句: if (list == null || list.size() == 0) {} if (list != null && l ...

  8. spring源码-bean之加载-2

    一.前面说了bean的容器初始化,后面当然是说bean的加载.这里还是不讲解ApplicationContext的bean的加载过程,还是通过最基础的XmlBeanFactory来进行讲解,主要是熟悉 ...

  9. Linux命令非常全

    最近都在和Linux打交道,感觉还不错.这也是很多人喜欢linux的原因,比较短小但却功能强大.我将我了解到的命令列举一下,仅供大家参考: 系统信息 arch 显示机器的处理器架构(1) uname ...

  10. mysql 优化笔记

    数据表总共81万条数 SQL explain ); 执行时间超级长,没有等到执行完成就终止了太慢了 explain一下 发现表bb 的select_type 为DEPENDENT SUBQUERY   ...