Repair the Wall (贪心)
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 (贪心)的更多相关文章
- --hdu 2124 Repair the Wall(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2124 Ac code : #include<stdio.h> #include<st ...
- HDU2124 Repair the Wall(贪心)
Problem Description Long time ago , Kitty lived in a small village. The air was fresh and the scener ...
- 简单贪心) Repair the Wall hdu2124
Repair the Wall http://acm.hdu.edu.cn/showproblem.php?pid=2124 Time Limit: 5000/1000 MS (Java/Others ...
- 杭电 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, ...
随机推荐
- python打印99乘法表
代码如下: print(XXX,end="\t") #表示打印不换行 附带python部分转义字符:
- Android中,子线程使用主线程中的组件出现问题的解决方法
Android中,主线程中的组件,不能被子线程调用,否则就会出现异常. 这里所使用的方法就是利用Handler类中的Callback(),接受线程中的Message类发来的消息,然后把所要在线程中执行 ...
- Scala基本语法总结(一)
Scala基本语法总结 时隔快一年了,又捡起scala,不是想学的有多么的精通,只是想把之前遇到的知识点和实践中遇到的重点进行归纳总结,对以后的面试或许有点帮助吧! 一.scala开发环境的配置 我这 ...
- Hive(5)-DDL数据定义
一. 创建数据库 CREATE DATABASE [IF NOT EXISTS] database_name [COMMENT database_comment] [LOCATION hdfs_pat ...
- SSH Secure :Algorithm negotiation failed,反复提示输入password对话框
在嵌入式开发中,SSH Secure File Transfer Client 软件使用,方便了windows和linux之间文件拷贝,尤其是多台主机状况下. 最近装了Ubuntu 16.0.4,在V ...
- SQL盲注
一.首先输入1和-1 查看输入正确和不正确两种情况 二.三种注入POC LOW等级 ... where user_id =$id 输入 真 and 假 = 假 (1)...where u ...
- (杭电2053)A + B Again(转换说明符)
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): ...
- mysql自动提交
MySQL的autocommit(自动提交)默认是开启,其对mysql的性能有一定影响,举个例子来说,如果你插入了1000条数据,mysql会commit1000次的,如果我们把autocommit关 ...
- [原创]利用python发送伪造的ARP请求
#!/usr/bin/env python import socket s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW) s.bind((&qu ...
- [原创]用python实现系统状态监测和故障转移
#coding: utf-8 import socket import select import time import os import threading def ser(): s = soc ...