Analysis of C/C++ (level five) real questions in December 2021#中国电子学院#National Youth Software Programming Level Examination

insert image description here

C/C++ Programming (Level 1~8) All real questions・Click here

Topic 1: Bookshelf

John recently bought a bookshelf to store his dairy farming books, but the shelf quickly filled up, leaving only the top shelf empty.
John has a total of N cows (1 ≤ N ≤ 20,000), each cow has its own height Hi (1 ≤ Hi ≤ 10,000), and the total height of N cows is S. The height of the bookshelf is B (1 ≤ B ≤ S < 2,000,000,007).
In order to reach the top level of the bookshelf, cows can step on the backs of other cows, like stacking arhats, until their total height is no less than the height of the bookshelf. Of course, the more cows there are, the greater the risk. To help John reach the top shelf, find a solution that uses the least number of cows.
Time limit: 10000
Memory limit: 65536
input
Line 1: Integers N and B separated by spaces Lines 2~N+1: Line i+1 outputs an integer Hi The
minimum
number of cows that can reach the height of the bookshelf is
input
6 40
6
18
11
13
19
11
Sample output
3

The following is the C language code to solve the bookshelf problem using the greedy algorithm:

Guess you like

Origin blog.csdn.net/gozhuyinglong/article/details/132549101