杭电2124Repair the Wall
2015-08-10 08:49
477 查看
Repair the Wall
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2775 Accepted Submission(s): 1344
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 ?
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).
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.
Sample Input
5 3
3 2 1
5 2
2 1
Sample Output
2
impossible
标准的贪心,题意就是说,给你一个L,给一个N,再给N个数,求最少需要多少个给的数,相加不小于N;把给的N个数 sort排序了,从大到小开始加,如果和大于等于L,就输出用了多少个数,如果加到最后都没有加到了
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; __int64 a[6000],i,n,m,j,k,l;//数据有点大,int不够; int cmp(int a,int b) { return a>b; } int main() { while(scanf("%I64d%I64d",&m,&n)!=EOF) { k=0; for(i=0;i<n;i++) { scanf("%I64d",&a[i]); k=k+a[i]; } if(k<m) printf("impossible\n");//直接判断能不能 else { sort(a,a+n,cmp);//从大到小排序 l=0; for(i=0;i<n;i++) { l=l+a[i]; if(l>=m) { printf("%d\n",i+1); break; } } } } return 0; }
相关文章推荐
- 快速排序
- C#快速排序算法实例分析
- C++快速排序的分析与优化详解
- php简单实现快速排序的方法
- Java 快速排序(QuickSort)原理及实现代码
- 快速排序和分治排序介绍
- java 算法之快速排序实现代码
- Java实现快速排序算法(Quicktsort)
- Java中的数组排序方式(快速排序、冒泡排序、选择排序)
- C/C++实现快速排序的方法
- c语言实现冒泡排序、希尔排序等多种算法示例
- 深入单链表的快速排序详解
- C++实现八个常用的排序算法:插入排序、冒泡排序、选择排序、希尔排序等
- JavaScript实现快速排序的方法
- C#使用委托实现的快速排序算法实例
- php实现快速排序的三种方法分享
- PHP两种快速排序算法实例
- 排序算法之PHP版快速排序、冒泡排序
- php排序算法(冒泡排序,快速排序)
- JavaScript实现快速排序(自已编写)