注解
1、贪心。贪心策略:降序排序。按顺序累加,直到大于等于sumValue。如果所有数加起来都不足sumValue,就输出Impossible。
代码
#include <iostream>
#include <algorithm>
using namespace std;
int compare(int x, int y){
return x>y;
}
int main() {
int L;
while(cin>>L){
int N;
cin>>N;
int a[N];
for(int i=0; i<N; i++){
cin>>a[i];
}
sort(a, a+N, compare);
int sumValue = 0;
int pos = 0;
while(sumValue<L && pos<N){
sumValue += a[pos];
pos++;
}
if(sumValue>=L){
cout<<pos<<endl;
}
else{
cout<<"impossible"<<endl;
}
}
return 0;
}