POJ 3268 Bookshelf 2 dynamic programming method, poj3268

Source: Internet
Author: User

POJ 3268 Bookshelf 2 dynamic programming method, poj3268

Description

Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly, and now the only available space is at the top.

FJ hasNCows (1 ≤N≤ 20) each with some heightHi(1 ≤Hi≤ 1,000,000-these are very tall cows). The bookshelf has a heightB(1 ≤BS, WhereSIs the sum of the heights of all cows ).

To reach the top of the bookshelf, one or more of the cows can stand on top of each other in a stack, so that their total height is the sum of each of their individual heights. this total height must be 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 is 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 shocould print the minimal 'excess' height between the optimal stack of cows and the bookshelf.

Input

* Line 1: Two space-separated integers:NAndB
* Lines 2 ..N+ 1: LineI+ 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

Source

USACO 2007 December Bronze

Solution:

1. determine the maximum possible height sum, that is, the total height of all cows.

2. Based on the dynamic programming method, the possible solution between sum of 1 and the maximum height is solved.

3. Find the lowest height than B, which may be the same as B.


#include <stdio.h>#include <vector>#include <limits.h>#include <string.h>#include <algorithm>using namespace std;const int MAX_N = 21, MAX_H = 1000000;int cow[MAX_N];bool height[MAX_N*MAX_H];int getMinHeight(int N, int B, int sum)//B < sum{fill(height, height+sum+1, false);height[0] = true;for (int i = 0; i < N; i++){for (int j = sum; j >= cow[i]; j--){if (height[j-cow[i]]) height[j] = true;}}int ans = B;for (; ans <= sum && !height[ans]; ans++) {}return ans;}int main(){int N, B, sum;while (~scanf("%d %d", &N, &B)){sum = 0;for (int i = 0; i < N; i++){scanf("%d", cow+i);sum += cow[i];}printf("%d\n", getMinHeight(N, B, sum)-B);}return 0;}




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.