• Bin Packing Problem(线段树维护最大值,map元素查找的应用)


    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 CC 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 TT, the number of cases.
    
    For each case, the first line of the input contains two integers n,C\ (1 \le n \le 10^6,\ 1 \le C \le 10^9)n,C (1≤n≤10 
    6
     , 1≤C≤10 
    9
     ), the number of items and the capacity of each bin. The second line contains nn integers, where the ii-th (1 \le i \le n1≤i≤n) integer a_i\ (1 \le a_i \le C)a 
    i
    ​
      (1≤a 
    i
    ​
     ≤C) denotes the volume of the ii-th item.
    
    It is guaranteed that the sum of nn over all cases doesn't exceed 10^610 
    6
     .
    
    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.
    
    Sample 1
    Inputcopy    Outputcopy
    2
    2 2
    1 1
    5 10
    5 8 2 5 9
    View Problem
    #include <bits/stdc++.h>
    using namespace std;
    #define ri register int
    #define M 1000005
    
    template <class G>void read(G &x)
    {
        x=0;int f=0;char ch=getchar();
        while(ch<'0'&&ch>'9'){f|=ch=='-';ch=getchar();}
        while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
        x=f?-x:x;
        return ;
     }
     
    int n,m;
    int T;
    int val[M];
    struct dian{
        int l,r;
        int mx;
    }p[4*M];
    void build(int l,int r,int i)
    {
        p[i].l=l;p[i].r=r;
        if(l==r)
        {
            p[i].mx=m;
            return ;
        }
        int mid=(l+r)>>1;
        build(l,mid,i<<1);
        build(mid+1,r,i<<1|1);
        p[i].mx=max(p[i<<1].mx,p[i<<1|1].mx);    
    }
    long long ans=0;
    void qu(int a,int i)
    {
        if(p[i].l==p[i].r)
        {
            if(p[i].mx==m) ans++;
            p[i].mx-=a;
            return ;
        }
        if(p[i<<1].mx>=a) qu(a,i<<1);
        else qu(a,i<<1|1);
        p[i].mx=max(p[i<<1].mx,p[i<<1|1].mx);
        
    }
    map<int,int> q;
    int main(){
        read(T);
        while(T--)
        {
            read(n);read(m);
            for(ri i=1;i<=n;i++)
            {
                read(val[i]);
            }
            
            build(1,n,1);
            ans=0;
            for(ri i=1;i<=n;i++)
            {
                qu(val[i],1);
            }
            printf("%lld ",ans);
            ans=0;
            q[m-val[1]]++;
            ans++;
            for(ri i=2;i<=n;i++)
            {
                map<int,int>:: iterator p=q.lower_bound(val[i]);
                if(p==q.end())
                {
                    q[m-val[i]]++;
                    ans++;
                }
                else
                {
                    q[p->first]--;
                    q[p->first-val[i]]++;
                    if(q[p->first]==0) q.erase(p);
                }
            }
            q.clear();
            printf("%lld\n",ans);
        }
        return 0;
        
    }  
    View Code

    反思:注意线段树的应用,维护最大最小值的位置。

              MAP 查找key值元素的应用。

  • 相关阅读:
    页面加载完没有其他操作的情况下直接获取音频时长为NAN问题
    关于mysql的一些操作
    阿里云服务器登录不上 提示:之前用于连接到 (公网ip) 的凭据无法工作(1核1G) 以及阿里云新版本安全组策略没有开启80端口导致网站只能ping通 访问不到的问题
    微信浏览器禁止页面下拉查看网址(不影响页面内部scroll)
    2018年11月17号第一次参加源创会记录
    使用了eclipse10年之后,我终于投向了IDEA
    spring/spring boot/spring cloud书籍推荐
    python数据库连接例子
    Spring Cloud Eureka配置文件例子与较为详细说明
    spring源代码下载并导入eclipse技巧
  • 原文地址:https://www.cnblogs.com/Lamboofhome/p/16005574.html
Copyright © 2020-2023  润新知