Topic link
Title description
Farmer John recently added a huge bookshelf to the cows' library. Even though it is so large, it is filled with various books almost instantly. Now, there is only a little space left on the top of the bookshelf.
All N (1 <= N <= 20,000) cows have a certain height H_i (1 <= H_i <= 10,000). Let the sum of the heights of all cows be S. The height of the bookshelf is B, and it is guaranteed that 1 <= B <= S <2,000,000,007.
In order to reach the top of the bookshelf higher than the tallest cow, the cows had to act like acrobatics, standing one on the back of the other, stacking them into a "cow tower". Of course, the height of this tower is the total height of all the cows in the tower. In order to put things on top of the bookshelf, the height of all cows must not be less than the height of the bookshelf.
Obviously, the more cows in the tower, the more unstable the entire tower, so the cows hope to keep the number of cows in the tower as small as possible on the premise that they can reach the top of the bookshelf. Now that the cows have found you, I hope you can help them calculate the minimum number.
Input format
Line 1: 2 integers separated by spaces: N and B * Line 2...N+1: Line i+1 is an integer: H_i
Output format
Line 1: Output 1 integer, that is At least how many cows are stacked into a tower to reach the top of the bookshelf.
Input and output example
Input #1
6 40
6
18
11
13
19
11
Output #1
3
Description/Prompt
Input description:
There are 6 cows in total, the height of the bookshelf is 40, and the height of the cows is between 6...19.
Output description:
A way to reach a height of 40 with only 3 cows: 18+11+13. Of course there are other methods, which are not listed here.
Code:
#include<iostream>
#include<algorithm>
using namespace std;
int num[20010];
int main()
{
int n, b, sum = 0, count = 0;
cin >> n >> b;
for(int i = 0; i < n; i++)
cin >> num[i];
sort(num, num + n); //从小到大排序
for(int i = n - 1; sum < b; i--, count++)
sum = sum + num[i];
cout << count;
return 0;
}