HDU 2,844 Multi-weight backpack

Source: Internet
Author: User

Background: The main idea is to understand the binary application. Next time you write a backpack nine you should make the code a little bit more and avoid mistakes.

Idea: F[i]=mas{f[i-kc[i]]+kw[i] | 0 =< k <= Mi}, for each capacity to seek its maximum value, because the value and cost is equal, so the value can only be less than equal to the cost, those values equal to the cost is the loss of words can constitute this number.

Learning: 1. Both value and expense are self-worth cases and can be used to determine whether these numbers can form another number.

#include <cstdio> #include <iostream> #include <cstring>using namespace Std;int c[109][2],f[100009];    int main (void) {int n,m;        while (scanf ("%d%d", &n,&m) && n*n+m*m) {for (int i=0;i < n;i++) scanf ("%d", &c[i][0]);        for (int i=0;i < n;i++) scanf ("%d", &c[i][1]<span id= "Transmark" ></span>);        memset (f,0,sizeof (F));                    for (int i=0;i < n;i++) {if (c[i][0]*c[i][1] >= m) {for (int j=c[i][0];j <= m;j++) {                F[j]=max (F[j],f[j-c[i][0]]+c[i][0]);                }}else{int K=1,key;                    while (K < c[i][1]) {key=k*c[i][0];                    for (int j=m;j >= key;j--) F[j]=max (F[j],f[j-key]+key);                    C[i][1]-=k;                k*=2;                } Key=c[i][1]*c[i][0];            for (int j=m;j >= key;j--) F[j]=max (F[j],f[j-key]+key); }} int count=0;        for (int i=1;i <= m;i++) if (f[i] = = i) count++;    printf ("%d\n", count); } return 0;}


HDU 2,844 Multi-weight backpack

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.