二、stl ,模拟,贪心等 [Cloned] U - 贪心

原题:

A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the same length l and each item i has length li<=l . We look for a minimal number of bins q such that 

  • each bin contains at most 2 items, 
  • each item is packed in one of the q bins, 
  • the sum of the lengths of the items packed in a bin does not exceed l .


You are requested, given the integer values n , l , l1 , ..., ln , to compute the optimal number of bins q . 

题意:

给出一些一维长度的棍子放进某最大长度为L 的箱子,每个箱子最多放两根,而且两根之和不能超过L,问至少需要多少个箱子。

题解:

先把棍子按照长度排好顺序,然后取当前最长的棍子和最短的棍子,如果两者之和小于L就把两根都放进去,如果大于L就只把长的那根放进去,直到放完所有的棍子。

代码:AC

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
bool compare(int a,int b)
{
	return a>b;
}
int A[100005];
int main()
{
	int n;
	cin>>n;
	int l;
	cin>>l;
	int i;
	for(i=0;i<n;i++)
		cin>>A[i];
	sort(A,A+n,compare);
	int sum=0;
	for(i=0;i<n;i++)
	{
		sum++;
		if(A[i]+A[n-1]<=l)
			n--;
	}
	cout<<sum<<endl;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值