题解:P13646 [NOISG 2016] LunchBox
1.题目大意
给定长度为
2.题目思路
考虑贪心。
要想让答案最大,我们就需要让
时间复杂度
3.代码
注:代码仅供参考。
:::info[代码]{open}
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int max_m=60002;
int n,m,k[max_m],l,ans;
long long sum;
int main(){
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d",&k[i]);
}
sort(k+1,k+m+1); //排序
l=1; //初始化
while(sum+k[l]<=n&&l<=m){
sum+=k[l]; //求和
ans++; //计数+1
l++; //指针+1
}
printf("%d\n",ans);
return 0;
}
:::
4.后记
更多内容,请移步至:
\color{red}\texttt{Luogu ryf2011} ;\color{orange}\texttt{cnblogs(博客园) cnblogs2011ryf} 。