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

#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; }
反思:注意线段树的应用,维护最大最小值的位置。
MAP 查找key值元素的应用。