When byteotian moved, they found that the handling of a large number of precision weights was an annoying task. The company has some fixed-capacity containers that can hold these weights. They want to attach as many weights as possible for handling and discard the remaining weights. Each container can have a limit on the number of weights, but the total weight they can hold cannot exceed the limit of each container. A container can also be left empty. Each of the two weights has a feature, and each of them has an integral multiple of the other weights. Of course, they may also be equal.
Solution
The key point is that the relationship between any two items is an integer multiple.
We can split each container in K-in-order, so we add items from small to large. If the current BIT is not satisfied, we will borrow from the high position.
Code
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define N 100002using namespace std;int a[N],b[N],c[N],tot,d[N],ji[N],n,m;int dfs(int id){ if(id>tot)return 0; if(d[id])return d[id]--,1; if(dfs(id+1)){ d[id]+=c[id+1]/c[id],d[id]--; return 1; } return 0; }int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;++i)scanf("%d",&a[i]); for(int i=1;i<=m;++i)scanf("%d",&b[i]); sort(b+1,b+m+1); for(int i=1;i<=m;++i){ if(b[i]!=b[i-1])c[++tot]=b[i]; ji[i]=tot; } for(int i=1;i<=n;++i) for(int j=tot;j>=1;--j){ d[j]+=a[i]/c[j];a[i]%=c[j]; } int now=1; for(int i=1;i<=m;++i){ if(!dfs(ji[i])){ printf("%d",i-1); return 0; } if(i==m)cout<<m; } return 0;}
[Poi2007] ODW-weights (Greedy)