Bin Packing poj2782(贪心)

Bin Packing poj2782

传送门 2782 – Bin Packing (poj.org)

题意

给定背包,至多装两样东西,所有东西都要被装起来,装起来的物品不得超过背包容量。问最少要几个背包。

思路

小的东西可以和大的东西拼在一起放进包里,这是优先情况;如果超容量了,那么就考虑只装大的,小的留着期待和下一个大的拼凑在一起,因为单独放一个小的比较浪费。

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
typedef long long ll;
inline ll rr(){ll f=1,x=0;char ch;do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');return f*x;}
using namespace std;
const int INF=0x3f3f3f3f;
const ll mod[2]={int(1e6 + 7), 998244353};
const int base[2]={29,31};
const int maxn=1e5+6;

int a[maxn];
int main(int argc, char const *argv[]) {
    int n=rr(),cap=rr(),ans=0;
    for(int i=0;i<n;i++) a[i]=rr();
    sort(a,a+n);
    int le=0,ri=n-1;
    while (le<=ri) {
        if(cap>=a[le]+a[ri]) ri--,le++,ans++;
        else if(cap>=a[ri]) ri--,ans++;
    }
    std::cout << ans << '\n';
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值