Poj 3628 bookshelf 2 0/1 backpack and DFS Solutions

Source: Internet
Author: User

Link: poj 3628 bookshelf 2

Bookshelf 2
Time limit:1000 ms   Memory limit:65536 K
Total submissions:7462   Accepted:3436

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


Question

The shelf height is B, and the nheaded dairy cows are stacked. If you want to find a number of cows, the stack height is greater than B. The size of the nheaded Dairy Cows must be as small as possible and the minimum possibility of solving the problems is higher.


Thought 1:

The value of N is not big. You can consider using DFS search and pruning. The idea is clear, but pruning is very heavy. Otherwise, it would be 20 to the power of 2.

Code:

# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; int A [30], CN [30], n, B, minex; void DFS (int x, int sum) {If (sum + CN [N]-CN [x] <B) return; // It is no longer possible to reach the top of the shelf if (sum> = B) {If (minex> sum-B) minex = sum-B; return;} DFS (x + 1, sum); DFS (x + 1, sum + A [x + 1]);} int main () {While (~ Scanf ("% d", & N, & B) {for (INT I = 1; I <= N; I ++) scanf ("% d ", & A [I]); CN [0] = 0; For (INT I = 1; I <= N; I ++) CN [I] + = cn [I-1] + A [I]; minex = 10000000; DFS (0, 0); printf ("% d \ n", minex );} return 0 ;}
Idea 2:

0/1 backpack, the total height of the cow plus B is the size of the backpack, fill the backpack as much as possible, the final result is the size of the backpack minus the maximum.

Note that the height of the cow is very large. To open a large array, memset will overwrite the memory, so let's continue with the for loop.

Code:

# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; int DP [20000010]; int main () {int N, B, H [22]; while (~ Scanf ("% d", & N, & B) {int sum = 0; For (INT I = 1; I <= N; I ++) {scanf ("% d", & H [I]); sum + = H [I];} int v = sum-B; // memset (DP, sizeof (DP); memset will exceed the memory Ah for (INT I = 0; I <= V; I ++) DP [I] = 0; for (INT I = 1; I <= N; I ++) for (Int J = V; j> = H [I]; j --) DP [J] = max (DP [J], DP [J-H [I] + H [I]); printf ("% d \ n ", v-DP [v]);} 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.