杭电2124Repair the Wall

来源:互联网 发布:java测试工程师面试题 编辑:程序博客网 时间:2024/03/28 16:05

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 33 2 15 22 1
 

Sample Output
2impossible
 


标准的贪心,题意就是说,给你一个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;}

0 0
原创粉丝点击