[Poi2007] ODW-weights (Greedy)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.