2014基本贪心1005
来源:互联网 发布:卧龙大数据招聘 编辑:程序博客网 时间:2024/06/07 13:31
Repair the Wall
Time Limit : 5000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 44 Accepted Submission(s) : 25
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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 ?
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
The 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 ith integer Ai(0<Ai<1000000000 ) means that the ith block has the size of 1×Ai (in inch).
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
For 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.
If Kitty could not repair the wall, just print "impossible" instead.
Sample Input
5 33 2 15 22 1
Sample Output
2impossibles
思路:
这题先把全部的加起来,看是不是大于或等于墙的大小,如果不是,输出“impossible”,否则从最大开始减。
代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>using namespace std;int cmp(int a,int b){ return a>b;}int main(){ long long int m,n; while(cin>>m>>n) { int i; int a[605]; long long int sum=0; for(i=0;i<n;i++) { cin>>a[i]; sum+=a[i]; } int k=0; sort(a,a+n,cmp); if(sum<m) { cout<<"impossible"<<endl; } else { for(i=0;i<n;i++) { if(m>=a[i]) { k++; m-=a[i]; } else { k++; m-=a[i]; } if(m<=0) { break; } } cout<<k<<endl; } } return 0;}
阅读全文
0 0
- 2014基本贪心1005
- 2014基本贪心1005
- 2014基本贪心1001
- 2014基本贪心1002
- 2014基本贪心1003
- 贪心基本介绍
- 基本算法------贪心
- 贪心算法的基本思想
- 贪心1005
- HDU 4864 Task(基本算法-贪心)
- HDU 4864 Task(基本算法-贪心)
- 贪心算法基本思想和代码框架
- 9种基本算法_贪心
- 基本算法——第四单元 贪心
- 贪心算法基本思想和典型例题
- 贪心法——基本思想
- 贪心算法基本思想和典型例题
- 贪心策略的基本内容和背包问题
- 吴军《数学之美》部分概念笔记(12章-END)
- Arduino控制motorshield
- Win10不支持承载网络的解决方法
- 关于android中主题的简介
- 深入理解JavaScript系列(27):设计模式之建造者模式
- 2014基本贪心1005
- mybatis中的#和$的区别
- Red and Black HDU
- Leetcode--Path Sum I,II,III
- 操作系统如何管理CPU资源?细说操作系统进程与多任务模型问题
- uva 10905
- 常用的20条Linux命令
- 遍历ArrayList和遍历Map
- Gym