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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值