P2676 [USACO07DEC]Bookshelf B

题目传送门

#include <bits/stdc++.h>

using namespace std;
const int N = 20010;
int a[N];


int main() {
    int n, b;
    cin >> n >> b;//b是书架的高度
    for (int i = 1; i <= n; i++) cin >> a[i];//每头牛的身高
    //按身高排序.从大到小
    sort(a + 1, a + 1 + n, greater<int>());

    int s = 0;//累加和
    for (int i = 1;; i++) {
        s += a[i];
        if (s >= b) {
            cout << i << endl;
            exit(0);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/littlehb/p/15593177.html