Repair the Wall (贪心)
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 ?
InputThe 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).
OutputFor 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 题目意思:Kitty的墙裂了,出现了一个长为L宽为1的矩形,幸好他身边有一些同样宽为1的矩形积木,但是这些积木的长度不确定,问你最少需要多少块
积木就能将墙补好。 解题思路:简单的贪心题目,贪心策略,将积木按长度从大到小排列,每次都先取最长的,直到所加的积木的长度大于墙缝即可,不需要恰好等于。 上代码:
#include<stdio.h>
#include<algorithm>
using namespace std;
int my_comp(int a,int b)
{
if(a>b)
return ;
else
return ;
}
int main()
{
int i,l,n,sum,count;
int a[];
while(scanf("%d%d",&l,&n)!=EOF)
{
sum=;
count=;
for(i=;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n,my_comp);
for(i=;i<n;i++)
{
sum=sum+a[i];
count++;
if(sum>=l)
{
break;
}
}
if(i>=n)
printf("impossible\n");
else
printf("%d\n",count); }
return ;
}
Repair the Wall (贪心)的更多相关文章
- --hdu 2124 Repair the Wall(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2124 Ac code : #include<stdio.h> #include<st ...
- HDU2124 Repair the Wall(贪心)
Problem Description Long time ago , Kitty lived in a small village. The air was fresh and the scener ...
- 简单贪心) Repair the Wall hdu2124
Repair the Wall http://acm.hdu.edu.cn/showproblem.php?pid=2124 Time Limit: 5000/1000 MS (Java/Others ...
- 杭电 2124 Repair the Wall(贪心)
Description Long time ago , Kitty lived in a small village. The air was fresh and the scenery was ve ...
- Repair the Wall
问题 : Repair the Wall 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Long time ago , Kitty lived in a small village. ...
- HDU 2124 Repair the Wall
http://acm.hdu.edu.cn/showproblem.php?pid=2124 Problem Description Long time ago , Kitty lived in a ...
- 1724: [Usaco2006 Nov]Fence Repair 切割木板( 贪心 )
倒过来看 , 每次总是选择最短的两块木板合并 , 用heap维护 ------------------------------------------------------------------- ...
- Fence Repair POJ - 3253 (贪心)
Farmer John wants to repair a small length of the fence around the pasture. He measures the fence an ...
- BZOJ 1724: [Usaco2006 Nov]Fence Repair 切割木板 贪心 + 堆 + 反向思考
Description Farmer John想修理牧场栅栏的某些小段.为此,他需要N(1<=N<=20,000)块特定长度的木板,第i块木板的长度为Li(1<=Li<=50, ...
随机推荐
- chromium之ScopedNSAutoreleasePool浅析
上代码,看看注释 ScopedNSAutoreleasePool只有Mac系统特有的,也可以理解为OC特有的函数, 其他系统为空实现 // On the Mac, ScopedNSAutorele ...
- 深入理解bit_or和bit_and,bit_count
bit_or:二进制数按位或,bit_and:二进制数按位与,bit_count:统计二进制数1个个数 下面以一个例子来说明用法:示例要实现的功能就是计算每月有几天有访问,先把示例摘录在这里.1234 ...
- mysql复制表结构和数据
1.复制表结构: create table newName like oldName;//可以复制所有结构. 或者: create table newName select * from oldNam ...
- redis整合Spring之序列化对象与反序列化
写在最前面 1.Spring必须是4.2.6及以上版本才支持redis 2.jar包版本建议统一 需要准备jar包 1.aopalliance-1.0.jar 2.spring-data-common ...
- QQ好友的价值玩法 及如何搞到几万好友?
我们知道,现在的自媒体平台太多了.微信公众号,企鹅媒体平台,今日头条.搜狐.UC.一点等等等. 但是现在的话最主要的就是盈利,我们很多朋友玩自媒体这个在很多平台都有自己的账号和大量的粉丝.但是,最后大 ...
- array of TVarRec 动态数组使用
FDQuery.AppendRecord()里是一个array of TVarRec.我们一般都是直接用[Var1,Var2,...].这样手工输入,但如果增加的元素我们预先不知道,就要声明一个arr ...
- hive常见的几种优化手段
Hive调优的几个入手点: Hive是基于Hadoop框架的,Hadoop框架又是运行在JVM中的,而JVM最终是要运行在操作系统之上的,所以,Hive的调优可以通过如下几个方面入手: 操作系统调优 ...
- PAT-A Java实现
1001 A+B Format (20) 输入:两个数a,b,-1000000 <= a, b <= 1000000 输出:a+b,并以每3个用逗号隔开的形式展示. 思路一: 1)计算出a ...
- 十分钟搭建和使用sonarqube代码质量管理平台
前言 Sonarqube为静态代码检查工具,采用B/S架构,帮助检查代码缺陷,改善代码质量,提高开发速度,通过插件形式,可以支持Java.C.C++.JavaScripe等等二十几种编程语言的代码质量 ...
- 20154327 Exp2 后门原理与实践
实践内容 使用netcat和socat.msf-meterpreter等工具获得主机权限,并进行一些恶意行为,如监控摄像头.记录键盘输入.截屏等. 详情见实验指导书 实践过程 netcat netca ...