HDU2124 Repair the Wall(贪心)
Problem Description
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
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
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
Author
Source
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int maxn=+;
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int len,n,i,res;
int a[maxn];
while(scanf("%d%d",&len,&n)!=EOF)
{
for(i=;i<n;i++)scanf("%d",&a[i]);
sort(a,a+n,cmp);
res=;
for(i=;i<n;i++)
{
if(a[i]>=len)
{
res++;
len-=a[i];
break;
}
else
{
len-=a[i];
res++;
}
}
if(len>)printf("impossible\n");
else printf("%d\n",res);
}
return ;
}
HDU2124 Repair the Wall(贪心)的更多相关文章
- --hdu 2124 Repair the Wall(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2124 Ac code : #include<stdio.h> #include<st ...
- 简单贪心) Repair the Wall hdu2124
Repair the Wall http://acm.hdu.edu.cn/showproblem.php?pid=2124 Time Limit: 5000/1000 MS (Java/Others ...
- Repair the Wall (贪心)
Long time ago , Kitty lived in a small village. The air was fresh and the scenery was very beautiful ...
- 杭电 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, ...
随机推荐
- Finding Lines
Finding Lines 题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...
- SUSE Linux 下redis 的坑
前面redis服务器安装-SuSE Linux Enterprise Server 11 SP3一章中安装好提示开放防火墙后的一些坑 前面漏了说明redis作为一个高速数据库,在互联网上,对应的安全机 ...
- 逃出克隆岛 (codevs 2059)
较普通的走迷宫的题 传送门 :codevs 2059 逃出克隆岛 思路 :BFS 即可 PS :传送门 不必重复使用 #include <iostream> #include < ...
- 单机Hadoop搭建
通过一段时间的学习,我在我的centos上安装了单机hadoop,如果这对你有帮助,就进来探讨学习一下 Hadoop伪分布式配置 Hadoop 可以在单节点上以伪分布式的方式运行,Hadoop 进程以 ...
- 从源码Build vim以及打包.deb
How to build vim 1. Build步骤 git clone --depth https://github.com/vim/vim.git # download the source c ...
- Python快捷键
IDLE默认不能显示行号,使用ALT+G 跳到对应行号,在右下角有显示光标所在行.列. ALT+P 上一个历史输入内容. ALT+N 下一个历史输入内容. IDLE中按F5可以运行代码.
- FZU 2238 Daxia & Wzc's problem
公式. $a×C_{m + i - 1}^m + d×C_{m + i - 1}^{m + 1}$. 推导过程可以看http://blog.csdn.net/queuelovestack/articl ...
- C#中的委托到底是什么概念??
委托,简单理解是这样的.比如您要管您的孩子,把孩子送进了幼儿园.OK.此时您就把您的孩子委托给了幼儿园.当幼儿园放学,将孩子交还给您的手中.则是委托的回调.当然我这里的例子是说异步委托调用.您也可以同 ...
- Makfile文件编写
一.make是什么 GNU make是一个工程管理器,专门负责管理.维护较多文件的处理,实现自动化编译.如果一个工程项目中,有成百上千个代码源文件,若其中一个或多个文件进过修改,make就需要能够自动 ...
- do{...}while(0)的意义和用法(转载)
linux内核和其他一些开源的代码中,经常会遇到这样的代码: do{ ... }while(0) 这样的代码一看就不是一个循环,do..while表面上在这里一点意义都没有,那么为什么要这么用呢? 实 ...