Logu P2676 super bookshelf, p2676 bookshelf
Description
Farmer John recently added a huge shelf to the library of cows. Although it was so big, it was almost instantly filled with a variety of books. Now, there is only a little space on the top of the shelf. All N (1 <= N <= 20,000) cows have a fixed height H_ I (1 <= H_ I <= 10,000 ). Set the sum of height for all cows to S. The shelf height is B and 1 <= B <= S <2,000,000,007. To reach the top of a shelf higher than the highest cow, the cows have to stand on the back of the other end and fold them into a "Tower of cows ". Of course, the height of this tower is the sum of the heights of all cows in the tower. To put things on top of the shelf, the height and height of all cows must be no less than the height of the shelf. Obviously, the more cows in the tower, the more unstable the whole tower, so the cows hope to keep as few cows as possible on the shelf. Now, the cows have found you and hope you can help them calculate the minimum number.
Input/Output Format
Input Format:
- Row 1st: two integers separated by spaces: N and B * 2nd .. N + 1: Row I + 1 is an integer: H_ I
Output Format:
- Row 3: output an integer, that is, the minimum number of cows stacked into a tower to reach the top of the shelf.
Input and Output sample input sample #1:
6 4061811131911
Output sample #1:
3
Description
Input description:
There are a total of 6 cows, the shelf height is 40, and the height of the cows is between 6 and 19.
Output description:
A method that uses only three cows to reach a height of 40: 18 + 11 + 13. Of course there are other methods, which are not listed here.
Greedy
Sort in order, start from a large enumeration, exit if conditions are met
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #define ls k<<1 7 #define rs k<<1|1 8 using namespace std; 9 const int MAXN=400400;10 inline void read(int &n)11 {12 char c=getchar();n=0;bool flag=0;13 while(c<'0'||c>'9') c=='-'?flag=1,c=getchar():c=getchar();14 while(c>='0'&&c<='9') n=n*10+c-48,c=getchar();flag==1?n=-n:n=n;15 }16 int n,h;17 int a[MAXN];18 int main()19 {20 read(n);read(h);21 for(int i=1;i<=n;i++) read(a[i]);22 sort(a+1,a+n+1);23 int now=0;24 for(int i=n;i>=1;i--)25 {26 now+=a[i];27 if(now>=h) { printf("%d",n-i+1); exit(0); }28 }29 return 0;30 }