E - Bin Packing Problem

E - Bin Packing Problem

Lzw is having the Operations Research class now. Today the teacher is talking about the bin packing problem and some approximation algorithms to solve it.

In the bin packing problem, items of different volumes must be packed into a finite number of bins with a fixed capacity C in a way that minimizes the number of bins used. In computational complexity theory, it is a combinatorial NP-hard problem.

There are two classical approximation algorithms.

First Fit Algorithm. Consider the items in input order and maintain a list of bins, initially empty. In each step, it attempts to place the current item in the first bin in the list that can accommodate the item. If no suitable bin is found, it appends a new bin at the end of the list and puts the item into the new bin.
Best Fit Algorithm. Consider the items in input order and maintain a list of bins, initially empty. In each step, it attempts to place the current item in the best bin that can accommodate the item. “Best” means that if there are more than one bins that can accommodate the item, the bin with the least remaining space is chosen. If no suitable bin is found, a new bin is added and the item is put into the new bin.
Please help Lzw implement these two algorithms and find out how many bins will each algorithm use.

Input
The input contains multiple cases. The first line of the input contains a single positive integer T, the number of cases.

For each case, the first line of the input contains two integers n,C (1≤n≤106, 1≤C≤109), the number of items and the capacity of each bin. The second line contains n integers, where the i-th (1≤i≤n) integer ai (1≤ai≤C) denotes the volume of the i-th item.

It is guaranteed that the sum of n over all cases doesn’t exceed 106.

Output
For each case, print a single line containing two integers, denoting the number of bins used by the First Fit and the Best Fit algorithm, respectively.

Example
Input
2
2 2
1 1
5 10
5 8 2 5 9
Output
1 1
4 3

#include<iostream>
#include<map>
using namespace std;
int n,c;
const int N=1e6+10;
struct tree{
int l,r;
int maxx;
}T[N<<2];
int a[N];
int quary(int node,int x)//查询
{
    if(T[node].l==T[node].r) return T[node].l;
    if(T[node<<1].maxx>=x) return quary(node<<1,x);
    else return quary(node<<1|1,x);
}
void uptree(int node,int u,int v)//单点修改
{
    if(T[node].l==T[node].r)
    {
        T[node].maxx+=v;
        return ;
    }
    int mid=T[node].l+T[node].r>>1;
    if(mid>=u) uptree(node<<1,u,v);
    else uptree(node<<1|1,u,v);
    T[node].maxx=max(T[node<<1].maxx,T[node<<1|1].maxx);
}
void build(int node,int l,int r)//建树
{
    T[node].l=l;
    T[node].r=r;
    if(l==r)
    {
        T[node].maxx=c;
        return ;
    }
    int mid=l+r>>1;
    build(node<<1,l,mid);
    build(node<<1|1,mid+1,r);
    T[node].maxx=max(T[node<<1].maxx,T[node<<1|1].maxx);
}
int main()
{
    int t;
    cin>>t;
    map <int,int>::iterator p;
    while(t--)
    {
        map <int,int> mp;
        int ans1=0,ans2=0;
        cin>>n>>c;
        build(1,1,n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",a+i);
            int pos=quary(1,a[i]);
            ans1=max(ans1,pos);
            uptree(1,pos,-a[i]);
        }
        for(int i=1;i<=n;i++)
        {
            p=mp.lower_bound(a[i]);
            if(p==mp.end()) mp[c-a[i]]++;
            else
            {
                mp[p->first]--;
                mp[p->first-a[i]]++;
                if(mp[p->first]==0) mp.erase(p);
            }
        }
        for(auto x:mp) ans2+=x.second;
        cout<<ans1<<' '<<ans2<<endl;
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 装箱问题(bin packing problem)是一种经典的组合优化问题,其目标是将一组物品(每个物品有不同的大小和价值)装入尽可能少的容器中,使得每个容器的总大小不超过容器的容量限制。这个问题在物流、制造业、计算机科学等领域都有广泛的应用。 ### 回答2: 垃圾桶装箱问题是一种经典的计算几何问题,也被称为装箱问题或轻便问题。这个问题的定义是,给定一个一定容量的垃圾桶,和一些需要被放置的物品,如何将这些物品尽可能地放置在垃圾桶中,使得最终的空隙最小?垃圾桶不能被卡满,所以问题也可以定义为给定一个一定容量的统一大小的垃圾桶,和一组物品,如何将这些物品最少地分组,使得每组物品的总体积不超过垃圾桶容量?这个问题有着广泛的应用,如在物流和制造业中对于运输和存储的优化。 该问题早在1960年代就已经被证明为NP-hard问题。也就是说,没有已知的多项式时间算法可以在所有情况下解决问题,因此需要通过一些启发式算法来逼近最优解。目前,许多启发式算法已经被开发出来来解决这个问题,其中最著名的算法是first-fit-decreasing(FFD)算法。它首先将所有物品按照体积从大到小排序,然后依次将每个物品放入已经放置好的组中,如果当前的组无法容纳该物品,则新建一个组。然后继续为下一个物品重复此过程,直到每个物品都被放置。 尽管FFD算法已经被证明在大多数情况下是有效的,但它仍然存在着一些局限性。例如,它不能保证总体积最小,并且随着物品数量的增加,运行时间也会快速增加。因此,研究者们一直在探索更好的算法来解决垃圾桶装箱问题。一些其他的算法包括best-fit、worst-fit、next-fit、first-fit等。 总之,垃圾桶装箱问题是一个重要的优化问题,虽然它是NP-hard的,但仍然有许多启发式算法可用于近似解决。这个问题有广泛的应用,如在制造和物流行业中对于运输和存储的优化。 ### 回答3: 装箱问题,又称为Bin Packing Problem,是一个经典的组合优化问题。在此问题中,我们需要将一组物品装入不同大小的容器(箱子)中,以最小化容器数量或最大化容器利用率。 在实际应用中,装箱问题广泛应用于各种物流与生产领域。例如,在物流配送中,需要将多个订单的货物装入尽可能少的运输箱中;在工厂生产中,需要将工件尽可能使用少的容器装运到下一工序,降低运输成本。 装箱问题有多种不同的变种,主要分为静态问题和动态问题。静态装箱问题指在事先已知物品和容器大小的情况下,最小化容器数量或最大化容器利用率。动态装箱问题则更加复杂,它涉及到物品逐个抵达,需要决定当前物品应放入哪个容器,以最小化总容器数量或最大化总容器利用率。 解决装箱问题的方法主要有贪心算法、启发式算法、精确算法等。其中,最常用的是贪心算法,虽然它不能保证得到最优解,但时间复杂度相对低,适用于处理大规模的问题。对于特定的装箱问题,则需要根据实际情况选择合适的方法进行求解。 总而言之,装箱问题是一个重要的组合优化问题,涉及到物流、生产等众多领域。解决该问题将有助于提高效率、降低成本,提升企业的竞争力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值