POJ 3627 Bookshelf (贪心)

Description

Farmer John recently bought a bookshelf for cow library, but the shelf is getting filled up quite quickly, and now the only available space is at the top.

Each of the N cows (1 ≤ N ≤ 20,000) has 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 each other in a stack, so that their total height is the sum of each of their individual heights. This total height must be no less than the height of
the bookshelf. Since more cows than necessary in the stack can be dangerous, your job is 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

解析: (水水更健康)

问题:已知有n个人及每个人的身高,问为了达到高度b,最少需要几个人叠罗汉。保证n个人的总身高超过b。

思路:先取身高最高的,如果不足b,再取剩下中最高的。先排序,再贪心。

代码:

#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;
}
时间: 2024-10-18 09:39:44

POJ 3627 Bookshelf (贪心)的相关文章

POJ 3628 Bookshelf 2 (01背包)

Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7496   Accepted: 3451 Description Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly, and now the only available

poj 1456 Supermarket (贪心+并查集)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int fa[10010]; struct node { int p; int d; }; struct node a[10010]; bool cmp(node a1,node a2)//利润从大到小 { return a1.p>a2.p; } int find(int x) { if(fa[x]

POJ 3628 Bookshelf 2 题解

本题解法很多,因为给出的数据特殊性故此可以使用DFS和BFS,也可以使用01背包DP思想来解. 因为一般大家都使用DFS,这里使用很少人使用的BFS,缺点是比DFS更加耗内存,不过优点是速度比DFS快. 当然也比DFS难写点: int N, B; int Height[21]; inline int mMin(int a, int b) { return a > b? b : a; } inline int mMax(int a, int b) { return a < b? b : a; }

POJ 1862 Stripies 贪心+优先队列

http://poj.org/problem?id=1862 题目大意: 有一种生物能两两合并,合并之前的重量分别为m1和m2,合并之后变为2*sqrt(m1*m2),现在给定n个这样的生物,求合并成一个的最小重量 思路: m1+m2 >=  2*sqrt(m1*m2) 所以每次取大的去合并,能变小. 直接优先队列就可以啦. #include<cstdio> #include<cmath> #include<queue> using namespace std;

poj 2431 Expedition 贪心+最大堆

当油量不够时从走过的油站中选最大加油量的 #include<iostream> #include<queue> #include<stdlib.h> #include<algorithm> using namespace std; #define MAX_N 10005 struct node{ int dist,fuel; }t[MAX_N]; bool cmp(const node &a,const node &b) { return a

POJ 3628 Bookshelf 2 0/1背包和DFS两种解法

题目链接:POJ 3628 Bookshelf 2 Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7462   Accepted: 3436 Description Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly,

poj 3298 Antimonotonicity 贪心

题意: 求一个序列的最大子序列,该子序列满足:a1>a2<a3>a4...... 分析: 贪心,从极大值起交替取这个序列中极小值.极大值. 代码: //poj 3298 //sep9 #include <iostream> using namespace std; const int maxN=30024; int a[maxN]; int main() { int cases; scanf("%d",&cases); while(cases--)

poj 3628 Bookshelf 2

Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10981   Accepted: 4860 Description Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly, and now the only availabl

poj 3404&amp;&amp;poj1700(贪心)

Bridge over a rough river Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4143   Accepted: 1703 Description A group of N travelers (1 ≤ N ≤ 50) has approached an old and shabby bridge and wishes to cross the river as soon as possible. Ho