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