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. MySQL必知必会 读书笔记一:简介

    了解数据库 数据库(database) 数据库(database) 保存有组织的数据的容器(通常是一个文 件或一组文件). 数据库软件应称为DBMS(数据库管理系统).数据库是通过DBMS创建和操纵的 ...

  2. 02JavaScript用法

    前言: 介绍一下javascript的最基础语法规范和用法. HTML 中的脚本必须位于 <script> 与 </script> 标签之间. 脚本可被放置在 HTML 页面的 ...

  3. JavaScript的兼容小坑和调试小技巧

    JavaScript作为一种弱类型编程语言,入门简单,只要稍微注意一下IE方面的兼容性,就可以很好的使用它. 本文主要是对IE兼容的小坑和调试的小技巧进行举例分析,并给出解决方法. 1.var str ...

  4. ethereum(以太坊)(十四)--Delete

    pragma solidity ^0.4.10; contract Delete{ /* delete可用于任何变量(除mapping),将其设置成默认值 bytes/string:删除所有元素,其长 ...

  5. SublimeText配置Python3运行环境

    1.查看python3安装路径which python3 2.打开sublime text 3,点击上部菜单栏Tools->Build System->new Build System 3 ...

  6. Leecode刷题之旅-C语言/python-141环形链表

    /* * @lc app=leetcode.cn id=141 lang=c * * [141] 环形链表 * * https://leetcode-cn.com/problems/linked-li ...

  7. Python入门 (三)

    迭代器与生成器 迭代器 迭代是Python最强大的功能之一,是访问集合元素的一种方式. 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器 ...

  8. [python]安装wxpython的时候遇到问题记录

    一.安装wxpython的时候报错 “no installation of python 2.7 found in registy” 解决方案: win7上,已经安装python27,但是在安装wxp ...

  9. CLR via C#读书笔记二:类型基础

    1.CLR允许将对象转换为它的(实际)类型或者它的任何基类型. 2.is操作符检测对象是否兼容于指定类型,is操作符永远不抛出异常. 3.as操作符返回对同一个对象的非null引用.如果对象不兼容,a ...

  10. CDN 缓存策略(转)

    1.CDN加速原理    通过动态域名解析,网友的请求被分配到离自己最快的服务器.CDN服务器直接返回缓存文件或通过专线代理原站的内容.    网络加速+内容缓存,有效提供访问速度 2.CDN节点数量 ...