Hdu2546: meal card (01 backpack)

Source: Internet
Author: User

Hdu2546: meal card
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2546

When we encounter problems, we need to select the value and order of the object. after sorting out the order, we need to process the object 01. This is because the smaller the first point, the closer we get to 5, and then we get the maximum value at a time, the more money we spend.

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>using namespace std;#define N 1005int dp[N+50];int main(void){    int num[N];    int n,m;    int i,j;    while(cin>>n&&n!=0){        for(i=1;i<=n;i++) cin>>num[i];        cin>>m;        memset(dp,0,sizeof(dp));        dp[0]=1;        int ma=0;        sort(num+1,num+1+n);        for(i=1;i<=n;i++){            for(j=m+45;j>=0;j--){                if((m-j)>=5&&dp[j]){                     dp[j+num[i]]=1;                    ma=max(ma,j+num[i]);                }            }        }                cout<<m-ma<<endl;    }    return 0;}

 

Hdu2546: meal card (01 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.