Title Link: http://poj.org/problem?id=3628
Test instructions
Problem Solving Ideas:
1 determine the maximum possible height sum, which is the height of all the cow added up
2 According to the dynamic programming method, the possible solution between the sum of 1 to the maximum height is solved
3 found the lowest height than B (Bookshelf height), which may be the same as B.
Sample Input 5 - 3 1 3 5 6 Sample Output 1
AC Code:
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <queue>5#include <algorithm>6#include <cmath>7#include <iostream>8 9 using namespacestd;TentypedefLong LongLL; One A #defineINF 0x3f3f3f3f - #defineN 1200000 - #defineMAXN 100000000 the #defineMoD 1000000007 - - intDp[n],a[n]; - + intMain () - { + intn,x,sum,i,j; A at while(SCANF ("%d%d", &n,&x)! =EOF) - { -Memset (DP,0,sizeof(DP)); -Memset (A,0,sizeof(a)); - -sum=0; in for(i=1; i<=n;i++) - { toscanf"%d", &a[i]); +sum+=A[i]; - } the * for(i=1; i<=n;i++) $ for(j=sum;j>=a[i];j--)Panax NotoginsengDp[j]=max (dp[j],dp[j-a[i]]+a[i]); - the for(i=1; i<=sum;i++) + { A if(dp[i]>=x) the { +printf"%d\n", dp[i]-x); - Break; $ } $ } - } - return 0; the}
POJ- -3628 Bookshelf 2