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. PHPStorm 配置命名空间

    文件-设置-Directories 选中:application     点击顶部:Sources,右侧会出现 Source Floders 配置项 点击:p进行设置 输入app\

  2. nargin

    nargin 编辑 nargin为“number of input arguments”的缩写. 在matlab中定义一个函数时, 在函数体内部, nargin是用来判断输入变量个数的函数.在matl ...

  3. Spring MVC 使用介绍(三)—— Controller接口控制器

    一.概述 Controller接口类图如下,其中,BaseCommandController已从Spring 4移除 基于继承Controller接口的方式已经不推荐使用,仅供学习参考 二.基于Con ...

  4. js判断一个元素是否在数组中

    js判断一个元素是否在数组中 var arr = ['a','s','d','f']; console.info(isInArray(arr,'a'));//循环的方式 function isInAr ...

  5. Tomcat启动特慢之SecureRandom问题解决

    tomcat启动日志: 08-Jun-2018 09:23:00.445 WARNING [localhost-startStop-1] org.apache.catalina.util.Sessio ...

  6. HTML中的元素定位

    static默认 relative相对定位 absolute绝对定位 mix relative and absolute混合相对定位和绝对定位 fixed固定定位 float浮动 reference ...

  7. linux系统版本大全

    Linux系统下载地址:http://www.jb51.net/LINUXjishu/239493.html linux系统教学视频:http://www.uplinux.com/shipin/lin ...

  8. POJ1151-扫面线+线段树+离散化//入门题

    比较水的入门题 记录矩形竖边的x坐标,离散化排序.以被标记的边建树. 扫描线段树,查询线段树内被标记的边.遇到矩形的右边就删除此边 每一段的面积是查询结果乘边的横坐标之差,求和就是答案 #includ ...

  9. BZOJ3277 串 【后缀数组】【二分答案】【主席树】

    题目分析: 用"$"连接后缀数组,然后做一个主席树求区间内不同的数的个数.二分一个前缀长度再在主席树上求不同的数的个数. 代码: #include<bits/stdc++.h ...

  10. 【XSY1528】azelso 概率&期望DP

    题目大意 有一条很长很长的路(出题人的套路),你在\(0\)位置,你要去\(h\)位置. ​ 路上有一些不同的位置上有敌人,你要和他战斗,你有\(p\)的概率赢.若你赢,则你可以走过去,否则你会死.还 ...