Question
有 nn 个题目,mm 分钟,做完每个题目所花费的时间是不一样的,求牛可乐最多可以做出多少个题目。
Solution
简单贪心
排个序,从小到大。
Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const ll maxn = 1e6 + 5;
const int N = 5e5 + 5;
ll n,m,a[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+1+n);
ll cnt=0;
for(int i=1;i<=n;i++){
if(m>=a[i]){
m-=a[i];
cnt++;
}else{
break;
}
}
cout<<cnt<<'\n';
return 0;
}
京公网安备 11010502036488号