joj2154 Hungry Rabbits

 2154: Hungry Rabbits


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s 8192K 1231 423 Standard
A group of hungry rabbits had got a basket of carrots, but these carrots isn't enough for all the rabbits to stay alive. So they had a meeting to solve the problem. They determined to drive off some rabbits,then the rest of them could have enough carrots. The number of carrots each rabbit required is given as a nonegetive interger vi, and there's T carrots in the basket. The chairman of rabbit association is a big beautiful and strong rabbit, he was very pity for doing this thing, and he wanted to know the least number of rabbits they would drive off to make the rest of the rabbits stay alive.

Input

There's more than one test case.In each case, there're two positive integers T and N. T stands for the number of carrots in the basket and N(1<=N<=1000) stands for the total number of the rabbits. Then following N lines each contains a nonegetive interger vi. These N intergers stand for the carrots each rabbit needed.

Output

For each test case ,output the least number of rabbits they would drive off!

Sample Input

30 5
12
14
23
4
10

Sample Output

2

 

Problem Source: sea

 


This problem is used for contest: 23 


Submit / Problem List / Status / Discuss

 

WA了一次,忘了考虑输出为0(即sum<=t)的情况。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int cmp(const void *a, const void *b)
 5 {
 6     return *(int *)a - *(int *)b;
 7 }
 8 
 9 
10 int main()
11 {
12     int t, n, i, sum;
13     int a[1005];
14     
15     while (scanf("%d%d", &t, &n) == 2)
16     {
17         sum = 0;
18         for (i=0; i<n; ++i)
19         {
20             scanf("%d", &a[i]);
21         }
22         qsort(a, n, sizeof(a[0]), cmp);
23         
24         for (i=0; i<n; ++i)
25         {
26             sum += a[i];
27             if (sum > t)
28             {
29                 printf("%d\n", n-i);
30                 
31                 break;
32             }
33             else if (i == n-1 && sum <= t)
34             {
35                 printf("0\n");
36             }
37         }
38     }
39     
40     
41     return 0;
42 }

 

posted @ 2012-05-03 22:33  漂木  阅读(328)  评论(0编辑  收藏  举报