1408121520-hd-Repair the Wall.cpp
来源:互联网 发布:良辰好景知几何txt下载 编辑:程序博客网 时间:2024/06/05 22:48
Repair the Wall
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2099 Accepted Submission(s): 1020
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 ?
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).
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.
If Kitty could not repair the wall, just print "impossible" instead.
Sample Input
5 33 2 15 22 1
Sample Output
2impossible
题目大意
台风破坏了小猫的房子,使他的房子出现了1*L的裂缝,正好小猫有N个宽度为1的小木块,小木块可以切割使用也可以完整使用,给出小木块的长度,求所需木块的最少数量
解题思路
这是一道贪心题,首先将小木块按照长度做降序排列,然后开始累加,若小木块总长度超过了裂缝长度则停止,输出此时用到的小木块的数量。
代码
#include<stdio.h>#include<algorithm>using namespace std;int s[650];bool cmp(int a,int b){ return a>b;}int main(){ int l,n; int i,j,sum,suml; while(scanf("%d%d",&l,&n)!=EOF) { for(i=0;i<n;i++) scanf("%d",&s[i]); sort(s,s+n,cmp); sum=0; suml=0; for(i=0;i<n;i++) { if(suml>=l) break; else { sum++; suml+=s[i]; } } if(suml>=l) printf("%d\n",sum); else printf("impossible\n"); } return 0;}
0 0
- 1408121520-hd-Repair the Wall.cpp
- Repair the Wall hd 2124
- hdu2124 Repair the Wall
- Repair the Wall+贪心
- Repair the Wall
- 2124:Repair the Wall
- Repair the Wall
- Repair the Wall
- Repair the Wall
- Repair the Wall
- Repair the wall
- hdu1214 Repair the Wall
- Repair the Wall
- Repair the Wall
- hdu 2124 Repair the Wall
- 题目24:Repair the Wall
- 题目24:Repair the Wall
- HDU 2124 Repair the Wall
- hadoop计算中的Shuffle过程
- android之DDMS翻译
- nyist oj 448 寻找最大数 (基础题)
- OJ 2779找朋友 BFS
- 23XML DTD
- 1408121520-hd-Repair the Wall.cpp
- Fragment中嵌套FragmentTabHost出错问题
- 从规划和监管机构和客户尽快解决尽快尽快
- AE开发之之ILayer接口
- a5源码 资源分享与下载
- iOS_截屏并裁剪
- C/C++中extern关键字详解
- 泰安治疗内分泌失调哪家好
- 微信轻松接入QQ客服