hdu 2124 Repair the Wall(贪心)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream>
2 #include<cstdio>
3 #include<algorithm>
4 using namespace std;
5 int A[1000];
6 int cmp(int x,int y)
7 {
8 return x>y;
9 }
10 int main()
11 {
12 int L,n;
13 while(scanf("%d %d",&L,&n)!=EOF)
14 {
15 int sum=0;
16 for(int i=1;i<=n;i++)
17 {
18 scanf("%d",&A[i]);
19 sum+=A[i];
20 }
21 sort(A+1,A+1+n,cmp);
22 if(sum<L)
23 printf("impossible\n");
24 else
25 {
26 sum=0;
27 int ans=0;
28 for(int i=1;i<=n;i++)
29 {
30 sum+=A[i];
31 ans++;
32 if(sum>=L)
33 {
34 printf("%d\n",ans);
35 break;
36 }
37 }
38 }
39 }
40 system("pause");
41 return 0;
42
2 #include<cstdio>
3 #include<algorithm>
4 using namespace std;
5 int A[1000];
6 int cmp(int x,int y)
7 {
8 return x>y;
9 }
10 int main()
11 {
12 int L,n;
13 while(scanf("%d %d",&L,&n)!=EOF)
14 {
15 int sum=0;
16 for(int i=1;i<=n;i++)
17 {
18 scanf("%d",&A[i]);
19 sum+=A[i];
20 }
21 sort(A+1,A+1+n,cmp);
22 if(sum<L)
23 printf("impossible\n");
24 else
25 {
26 sum=0;
27 int ans=0;
28 for(int i=1;i<=n;i++)
29 {
30 sum+=A[i];
31 ans++;
32 if(sum>=L)
33 {
34 printf("%d\n",ans);
35 break;
36 }
37 }
38 }
39 }
40 system("pause");
41 return 0;
42