HDOJ-2124 Repair the Wall
来源:互联网 发布:seo自媒体 编辑:程序博客网 时间:2024/06/03 16:12
Repair the Wall
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3509 Accepted Submission(s): 1734
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
贪心算法,输出时注意impossible的问题,在木板长度小于漏洞长度时,输出impossible!!!
代码如下:
#include <cstdio>#include <algorithm>#include<stdint.h>#include<math.h>#include<string.h>int p[605];bool cmp(int x,int y){ return x>y;}using namespace std;int main(){ int l,n,a; while(~scanf("%d%d",&l,&n)){ if(!n){ printf("impossible\n"); continue; } a=0; memset(p,0,sizeof(p)); for(int i=0;i<n;i++){ scanf("%d",&p[i]); } sort(p,p+n,cmp); for(int i=0;i<n;i++){ if(p[i]>=l){ a++; l-=p[i]; break; } else{ l-=p[i]; a++; } } if(l<=0) printf("%d\n",a); else printf("impossible\n"); } return 0;}
- HDOJ 2124 Repair the Wall
- hdoj 2124 Repair the Wall
- HDOJ 2124 Repair the Wall
- HDOJ 2124 Repair the Wall
- HDOJ-2124 Repair the Wall
- HDOJ 2124 Repair the Wall
- hdoj-【2124 Repair the Wall】
- hdoj 2124 Repair the Wall 【贪心】
- HDOJ-2124之Repair the Wall
- HDOJ Repair the Wall JAVA 2124
- 2124:Repair the Wall
- HDOJ 2124 Repair the Wall(修城墙,贪心)
- hdoj 1010 Repair the Wall(贪心)
- hdu 2124 Repair the Wall
- HDU 2124 Repair the Wall
- hdu 2124 Repair the Wall
- hdu 2124 Repair the Wall
- hdu 2124 Repair the Wall
- 增强型for循环
- Java 类的热替换——概念、设计与实现
- string 序列化
- LINUX执行定时任务
- Debian 7 和 Debian 8 用户安装 Java 8的方法
- HDOJ-2124 Repair the Wall
- linux(一)-concept,catalogue
- android 给button 设置做图标注意
- python开发_platform_获取操作系统详细信息工具
- UITabelView自适应高度那些事
- 史上最详细测试用例设计实践-2:解决方案和总结
- CSS 文本垂直居中
- 古往今来中国最经典的50句名言
- StringBuffer,数组排序,Arrays,Character