Description
Farmer John recently bought another bookshelf for the Cow library, but the shelf was getting filled up quite quickly, and N ow the only available space was at the top.
FJ have n cows (1≤ n ≤20) each with some height of hi (1≤ hi ≤1,000,000-these is Very tall cows). The bookshelf has a height of b (1≤ b ≤ S, where S are the sum of the heights of all C OWS).
To reach the top of the bookshelf, one or more of the cows can stand on top of each and the a stack, so that their total Height is the sum of each of their individual heights. This total height must is no less than the height of the bookshelf in order for the cows to reach the top.
Since a taller stack of cows than necessary can be dangerous, your job was to find the set of cows that produces a stack of The smallest height possible such that the stack can reach the bookshelf. Your program should print the minimal ' excess ' height between the optimal stack of cows and the bookshelf.
Input
* Line 1:two space-separated integers: N and B
* Lines 2. N+1:line i+1 contains a single integer: Hi
Output
* Line 1: A single integer representing the (non-negative) difference between the total height of the optimal set of cows And the height of the shelf.
Sample Input
5 1631356
Sample Output
1
The main topic: there is a bookshelf of n cow, Bookshelf is too high to stand on the cow enough bookshelf, the height of the known n cattle and the height of the bookshelf, a cow can stand on the other side of the cow (Newton's coffin plate in the shaking), the total height is their height and the height and need not less than (greater than equal) bookshelf Ask what is the minimum height to meet the requirement, and output the difference between this height and the bookshelf height.
Parsing: Can be poor to cite all heights, and then traverse the judgment to meet the criteria of the value of the solution, just this problem data is not 20, can be violent enumeration, code I do not post it. Recently in the study DP, this problem can be deformed to 01 knapsack problem, DP storage cow height and backpack capacity V for all the height of the cattle sum, value and volume is the height of each cow.
1#include <iostream>2#include <stdio.h>3#include <math.h>4#include <string.h>5#include <ctype.h>6#include <stdlib.h>7#include <queue>8#include <vector>9#include <stack>Ten#include <algorithm> One using namespacestd; A #defineOO 0x3f3f3f3f - #defineMAXN 1000010 - the intdp[maxn],val[ -]; - - intMain () - { + intn,k; - while(SCANF ("%d%d", &n,&k)! =EOF) + { A intsum=0; at for(intI=1; i<=n; i++) - { -scanf"%d",&val[i]); -sum+=Val[i]; - } - intmin=Oo; in for(intI=1; i<=n; i++) - for(intJ=sum; j>=val[i]; j--) to { +Dp[j]=max (dp[j],dp[j-val[i]]+val[i]); - if(dp[j]>=k)//Here is >=, English is not good, only > W once the { *min=min (min,dp[j]); $ }Panax Notoginseng } -printf"%d\n", min-k); the } + return 0; A}
On the internet to see the giant's posts, found can also use search to write
1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd; 5 inth[ A], ans, flag; 6 intN, M; 7 voidDfsintKints)8 { 9 if(s = =m)Ten { OneAns =0; A return ; - } - if(S >=m) the { - if(S-m <ans) -Ans = s-m; - return ; + } - for(inti = k; I < n; i++) + { ADFS (i+1, s+H[i]); at } - } - intMain () - { - inti; - while(Cin >> N >>m) in { - intsum =0; toFlag =0; + for(i =0; I < n; i++) - { theCIN >>H[i]; *Sum + =H[i]; $ } Panax Notoginseng if(Sum = =m) - { thecout <<"0"<<Endl; + Continue; A } theAns =sum; +Dfs0,0); -cout << ans <<Endl; $ } $ return 0; -}
The code goes from http://blog.csdn.net/lyhvoyage/article/details/17042369
Bookshelf 2 (POJ 3628)