Repair the Wall

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

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6833    Accepted Submission(s): 3174

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
 
c++代码:
 
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll a[];
bool cmp(ll a,ll b){ //用这个来进行降序排列吧,用greater<int>()可能会没过。
return a>b;
}
int main(){
ll len,m;
while(~scanf("%lld%lld",&len,&m)){
for(int i = ;i < m;i++){
scanf("%lld",&a[i]);
}
sort(a,a+m,cmp);
ll sum = ;
ll cnt = ;
int i;
for (i = ;i<m;i++){
sum += a[i];
cnt++;
if(sum>=len){
break;
}
}
if(i==m){
printf("impossible\n");
}
else{
printf("%d\n",cnt);
}
}
return ;
}

简单贪心) Repair the Wall hdu2124的更多相关文章

  1. HDU2124 Repair the Wall(贪心)

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

  2. Repair the Wall (贪心)

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

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

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

  4. CF 628C --- Bear and String Distance --- 简单贪心

    CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...

  5. Uva 11729 Commando War (简单贪心)

    Uva 11729  Commando War (简单贪心) There is a war and it doesn't look very promising for your country. N ...

  6. Repair the Wall

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

  7. CDOJ 1502 string(简单贪心)

    题目大意:原题链接 相邻两个字母如果不同,则可以结合为前一个字母,如ac可结合为a.现给定一个字符串,问结合后最短可以剩下多少个字符串 解体思路:简单贪心 一开始读题时,就联想到之前做过的一道题,从后 ...

  8. HDU 2124 Repair the Wall

    http://acm.hdu.edu.cn/showproblem.php?pid=2124 Problem Description Long time ago , Kitty lived in a ...

  9. ACM_发工资(简单贪心)

    发工资咯: Time Limit: 2000/1000ms (Java/Others) Problem Description: 作为广财大的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日 ...

随机推荐

  1. easyui 自动动态合并单元格

    .......onLoadSuccess : function(data) { if (data.rows.length > 0) { //调用mergeCellsByField()合并单元格 ...

  2. WebAPI MVC Change Identity Default Table

    看过之前的文章小伙伴们应该已经明白了,当我们新建一个带有身份验证的模板时,会自带Identity Server,并且它的表名和字段名也都是默认的. 那么该如何修改它,并让EF知道呢?不废话,直接上代码 ...

  3. CF821C Okabe and Boxes

    题目链接 题目大意 模拟栈的操作,要求从1~n依次弹出,若不符合可以排序,且不会有不合法情况,求最少排序次数. 思路 由于一定没有不合法情况,所以每次不符合顺序时可以直接清空,遇到栈为空时仍然要求弹出 ...

  4. JVM是如何处理异常的

    JVM处理异常 异常处理的两大组成要素是抛出异常和捕获异常.这两大要素共同实现程序控制流的非正常转移. 抛出异常可分为显式和隐式两种.显式抛异常的主体是应用程序,指的是在程序中使用throw关键字,手 ...

  5. linux-shell系列5-统计

    #!/bin/bashshow=$(service --status-all 2>/dev/null | grep -E "is running|正在运行"|awk '{pr ...

  6. DRF 分页组件

    Django Rest Framework 分页组件 DRF的分页 为什么要使用分页 其实这个不说大家都知道,大家写项目的时候也是一定会用的, 我们数据库有几千万条数据,这些数据需要展示,我们不可能直 ...

  7. win10安装MySql 5.7.23

    下载安装 因为Django2.1不再支持MySQL5.5,这里需要重新安装一下MySQL 首先去官网下载 这里使用的是msi版本 https://dev.mysql.com/downloads/win ...

  8. [NOIp2009] $Hankson$ 的趣味题

    类型:数论 传送门:>Here< 题意:给出四个数$a_0,a_1,b_0,b_1$,求满足$gcd(x,a_0)=a_1,lcm(x,b_0)=b_1$的$x$的个数 解题思路 显然$a ...

  9. Python课程目录

    Python基础1 介绍.基本语法.流程控制 Python基础2 列表.字典.集合 Python基础3 基础3-函数

  10. pycharm修改注释颜色

    原来的注释是红色的,看着跟报错似的.. 还有flask中html文件的注释,我修改了Django的注释颜色,flask也就改了 也可以直接点击下面的代码,哪里难看点哪里