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, ...
随机推荐
- 初识JavaWEB
小荷才露尖尖角,早有蜻蜓立上头 首先,你需要知道你要学习哪些知识,掌握哪些技术?且听我娓娓道来. 第一阶段:HTML ,CSS ,JavaScript这三个是必须要掌握的,也是最基础的了.其实HEML ...
- html的map自适应
Map常识 请自己看吧:http://www.w3school.com.cn/tags/tag_map.asp Map自适应 <!DOCTYPE html> <html> &l ...
- 数据挖掘_wget整站下载
你应该了解的所有wget命令 翻译自All the Wget Commands You Should Know 如何下载整个网站用来离线浏览?怎样将一个网站上的所有MP3文件保存到本地的一个目录中?怎 ...
- getResourceAsStream和getResource的用法
用JAVA获取文件,听似简单,但对于很多像我这样的新人来说,还是掌握颇浅,用起来感觉颇深,大家最经常用的,就是用JAVA的File类,如要取得 D:/test.txt文件,就会这样用File file ...
- 时间序列 预测分析 R语言
在对短期数据的预测分析中,我们经常用到时间序列中的指数平滑做数据预测,然后根据不同. 下面我们来看下具体的过程 x<-data.frame(rq=seq(as.Date('2016-11-15' ...
- win8及win8.1商店出现0X80073CF9的解决办法!
//添加appinstallagent无效的方法 http://jingyan.baidu.com/article/e52e3615a2b38f40c60c51d3.html
- ubuntu16.04 samba 配置
samba是一个很有用的在Linux和Windows之间共享文件的服务器程序,在工作的时候一直在使用,不过都是别人配置好的环境,自已一直没有配置过Samba服务器,今天尝试着自己配置的一次遇到了很多的 ...
- JAVA上连接ubuntu14.04上的Hbase
对于新手来说,连接虚拟机上的Hbase有点繁琐,而且网上的配置不太适合初学者,今天我就整理了一下,希望对你们有帮助,第一次发博客. 1.首先去官网下载Hbase的压缩包.我这里用的是1.2.1 htt ...
- strutsprepareAndExecuteFiter
FilterDispatcher是早期struts2的过滤器,后期的都用StrutsPrepareAndExecuteFilter了,如 2.1.6.2.1.8.StrutsPrepareAndExe ...
- sql数据导出导入格式化
SHELL脚本导入导出数据时,按逗号分隔,引号包含字段,null值'' gccli -uroot -h$ip -e"rmt:${sqlStr} into outfile '${name}${ ...