POJ 3628 Bookshelf 2 Basic 01 Backpack

Source: Internet
Author: User

The main topic: FJ has n cows, and a high-h shelf, give each cow height, ask the cow to stack up over the shelf of the minimum height is how much.

Topic Idea: Find out all the heights that the cow can reach, and save it with dp[], and finally go through it, find out the dp[with the least H difference) is the answer.

#include <cstdio>#include<stdio.h>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<cstring>#include<vector>#include<queue>#defineINF 0x3f3f3f3f#defineMAX 2000005using namespacestd;intDp[max],a[max],sum,ans;voidInit () {sum=0; Ans=INF; Memset (DP,0,sizeof(DP));}intMain () {inti,j,maxn,n,h;  while(SCANF ("%d%d", &n,&h)! =EOF)        {Init ();  for(i=1; i<=n;i++) {scanf ("%d", &a[i]); sum+=a[i];}  for(i=1; i<=n;i++)        {             for(j=sum;j>=a[i];j--) {Dp[j]=max (dp[j],dp[j-a[i]]+A[i]); }        }         for(i=1; i<=sum;i++)        {            if(Dp[i] >= h)//If the condition is met, the comparison continues, keeping the smaller value{ans=min (ans,dp[i]-h); }} printf ("%d\n", ans); }    return 0;}
View Code

POJ 3628 Bookshelf 2 Basic 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.