原题链接:https://www.luogu.com.cn/problem/P2676
题意解读:要使能够到书架顶的牛数量最少,优先选高的牛即可,直到总身高超过书架高度,简单的排序+贪心,下面给出代码。
100分代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 20005;
int h[N];
int n, b;
int main()
{
cin >> n >> b;
for(int i = 1; i <= n; i++) cin >> h[i];
sort(h + 1, h + n + 1, greater<int>());
int sum = 0, cnt = 0;
for(int i = 1; i <= n; i++)
{
sum += h[i];
cnt++;
if(sum >= b) break;
}
cout << cnt;
return 0;
}
标签:P2676,int,洛谷题,Bookshelf,排序,USACO07DEC From: https://www.cnblogs.com/jcwy/p/17996565