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 ?

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 (贪心)的更多相关文章

  1. --hdu 2124 Repair the Wall(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2124 Ac code : #include<stdio.h> #include<st ...

  2. HDU2124 Repair the Wall(贪心)

    Problem Description Long time ago , Kitty lived in a small village. The air was fresh and the scener ...

  3. 简单贪心) Repair the Wall hdu2124

    Repair the Wall http://acm.hdu.edu.cn/showproblem.php?pid=2124 Time Limit: 5000/1000 MS (Java/Others ...

  4. 杭电 2124 Repair the Wall(贪心)

    Description Long time ago , Kitty lived in a small village. The air was fresh and the scenery was ve ...

  5. Repair the Wall

    问题 : Repair the Wall 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Long time ago , Kitty lived in a small village. ...

  6. HDU 2124 Repair the Wall

    http://acm.hdu.edu.cn/showproblem.php?pid=2124 Problem Description Long time ago , Kitty lived in a ...

  7. 1724: [Usaco2006 Nov]Fence Repair 切割木板( 贪心 )

    倒过来看 , 每次总是选择最短的两块木板合并 , 用heap维护 ------------------------------------------------------------------- ...

  8. Fence Repair POJ - 3253 (贪心)

    Farmer John wants to repair a small length of the fence around the pasture. He measures the fence an ...

  9. BZOJ 1724: [Usaco2006 Nov]Fence Repair 切割木板 贪心 + 堆 + 反向思考

    Description Farmer John想修理牧场栅栏的某些小段.为此,他需要N(1<=N<=20,000)块特定长度的木板,第i块木板的长度为Li(1<=Li<=50, ...

随机推荐

  1. idea 引入多项目

    1.先导入总包 2.右侧mavenmaven,选择parent的pom.xml 3.右上角“Project Structure”检查SDK

  2. Jquery拼图

    Jquery代码 <script> $(function () { $("td").click(function () { var img = $(this).prop ...

  3. 如何给ioloop.run_sync()中调用的函数传入参数

    问题 如何给tornado.ioloop.IOLoop中的run_sync方法中调用的函数添加参数 解决方案 使用functools.partial 解决示例 from tornado import ...

  4. Python文本和字符串常用操作

    ## 字符串分割 line = "This is my love!" fields = line.split(' ') print(fields) # ['This', 'is', ...

  5. try catch finally 中 returne的执行顺序

    结论:1.不管有没有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...

  6. 【blockly教程】第五章 循环结构

    在这里,我们将介绍一个新游戏--Pond Tutor 在Pond Tutor(https://blockly-games.appspot.com/pond-tutor)这个游戏中,我们将扮演黄色的鸭子 ...

  7. <简明>Markdown指南

    什么是Markdown?Markdown是一种轻量级的「标记语言」,通常为程序员群体所用,目前它已是全球最大的技术分享网站 GitHub 和技术问答网站 StackOverFlow 的御用书写格式. ...

  8. 微服务架构(Microservice Architect Pattern)综述——什么是微服务架构(读书笔记)

    简单定义: 微服务架构是一种架构模式,它提倡将单一应用程序划分成一组小的服务,服务之间相互协调,相互配合,为用户提供最终价值.每个服务运行在其独立的进程中,服务与服务间采用轻量级的通信机制相互沟通(通 ...

  9. Unicode编码相关概念

    1.Unicode是一种字符映射方案,这种映射并不是编码(即还没有到二进制机器码层面),而是像一个电话本一样,把全世界所有语言使用的字符,都映射成一个"u+"开头的数字(在JAVA ...

  10. springboot与activemq的使用

    1.springboot和activemq的使用相对来说比较方便了,我在网上看了很多其他的资料,但是自己写出来总是有点问题所以,这里重点描述一下遇到的一些问题. 2.至于activemq的搭建和spr ...