POJ 3627 Bookshelf (greedy)

Source: Internet
Author: User

Description
Farmer John recently bought a bookshelf for Cow library, but the shelf was getting filled up quite quickly, and now the onl Y available space is at the top.
Each of the N cows (1≤n≤20,000) have some height of Hi (1≤hi≤10,000) and a total height summed across all N cows of S. The bookshelf has a height of B (1≤b≤s < 2,000,000,007).
To reach the top of the bookshelf taller than the tallest cow, one or more of the cows can stand on top of all other in a Stack, so, their total height are the sum of each of their individual heights. This total height must is no less than the height of the bookshelf. Since more cows than necessary in the stack can be dangerous, your job was to find the set of cows that produces a stack of The smallest number of cows possible such that the stack can reach the bookshelf.

Input
* Line 1:two space-separated integers:n and B
* Lines 2..n+1:line i+1 contains a single Integer:hi

Output
* Line 1: A single integer representing the size of the smallest set of cows that can reach the bookshelf.

Sample Input
6 40
6
18
11
13
19
11
Sample Output

3


Analysis: (Water and water more healthy)

Question: It is known that there are n individuals and each person's height, asking for a minimum of several people to dogpile in order to reach the height B. Ensure that the total height of the n person exceeds B.

Idea: Take the highest height first, if less than B, then take the remaining highest. First sort, then greedy.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <set > #include <climits> #include <cmath> #include <algorithm> #define MAXN 50010#define RST (n) memset (n , 0, sizeof (N)) using namespace Std;int A[MAXN], N, m;int cmp (const void *a, const void *b) {    return * (int *) A-* (int * ) b;} int main () {while    (~scanf ("%d%d", &n, &m)} {for        (int i=0; i<n; i++) scanf ("%d", &a[i]);        Qsort (A, n, sizeof (int), CMP);        for (int i=n-1; i>=0; i--) {            m-= a[i];            if (M <= 0) {                printf ("%d\n", n-i);                Break    ;    }}} return 0;}


POJ 3627 Bookshelf (greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.