Problem Descriptionlong time ago, Kitty lived in a small village. The air was fresh and the scenery were very beautiful. The only thing that troubled she 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 is that's all of the Kitty's walls were made of wood.
One day, Kitty found that there is a crack in the wall. The shape of the crack is
A rectangle with the size of 1xL (inch). Luckly Kitty got N blocks and a saw (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 some of the blocks (of course she could use it directly without cutting) and Put them in the crack, and the wall is 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 yo U help her? Inputthe problem contains many test cases, please process to the end of file (EOF).
Each test case contains the lines.
In the first line, there is integers L (0<l<1000000000) and N (0<=n<600) which
mentioned above.
In the second line, there is N positive integers. The ith integer Ai (0<ai<1000000000) means that the ith block have the size of 1xAi (in inch). Outputfor each test case, print an integer which represents the minimal number of blocks is needed.
If Kitty could not repair the wall, just print "impossible" instead. Sample Input
5 33 2 15 22 1
Sample Output
2impossible
Authorlinlesourcehdu 2007-10 Programming Contest
#include <stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>#include<algorithm>using namespacestd;Const intmaxn= -+Ten;BOOLcmpintAintb) { returnA>b;}intMain () {intLen,n,i,res; intA[MAXN]; while(SCANF ("%d%d", &len,&n)! =EOF) { for(i=0; i<n;i++) scanf ("%d",&A[i]); Sort (A,a+n,cmp); Res=0; for(i=0; i<n;i++) { if(a[i]>=Len) {Res++; Len-=A[i]; Break; } Else{len-=A[i]; Res++; } } if(len>0) printf ("impossible\n"); Elseprintf"%d\n", RES); } return 0;}
HDU2124 Repair the Wall (greedy)