您的位置:首页 > 其它

(Relax ST1.19)POJ 3627 Bookshelf(贪心)

2013-11-28 22:03 411 查看
问题:已知有n个人及每个人的身高,问为了达到高度b,最少需要几个人叠罗汉。保证n个人的总身高超过b。

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

/*
* POJ_3627.cpp
*
*  Created on: 2013年11月26日
*      Author: Administrator
*/

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 20005;
int a[maxn];

bool cmp(int a,int b){
return a>b;
}

int main(){
int n,b;
while(scanf("%d%d",&n,&b)!=EOF){
int i;
for(i = 0 ; i < n ; ++i){
scanf("%d",&a[i]);
}

sort(a,a+n,cmp);

int count = 0;
int sum = 0;
for(i = 0 ; i < n ; ++i){
sum += a[i];
count++;

if(sum >= b){
break;
}

}

printf("%d\n",count);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: