Solution to a problem - super bookshelf

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

[USACO 2007 Dec] super bookshelf

The Description
Farmer John has recently acquired a huge library bookshelf for the cows, although it is so big, but it is almost instantly be filled with all kinds of books. Now, only the top of the bookshelf also left little space. All N (1 <= N <= 20) have a certain cows height H_i (1 <= H_i <= 1,000,000 - cows good high> _ <). All set for the cows height and S. Height of shelves is B, and to ensure that 1 <= B <= S. In order to reach the other end is even higher than the highest of the top shelves of dairy cows, the cows had to perform acrobatics like a general, one standing on the back of another head, folded into a "cow tower." Of course, the height of the tower, the tower is the sum of all the cow's height. In order to put things on top of the shelves, all the cows and height must not be less than the height of the shelves. The higher the stack would tower more unstable, so the cows are hoping to find a program that fold out of the tower at a height of not less than the height of the shelves under the circumstances, the height as small as possible. You can also guess your task: Write a program that calculates cows quartet built tower in the case meet the requirements, a minimum number of shelves than high.
Input

  • Line 1: 2 with a space-separated integers: N and B
  • 2 ... N + 1 of the line: first line i + 1 is an integer 1: H_i
    the Output
  • Line 1: Output a non-negative integer, namely cows quartet built the tower higher than the minimum height of the shelves
    the Sample the Input
    5 16
    3
    1
    3
    5
    6
    the Sample the Output
    17
    // we use cows 1,3,4,5 Diecheng column, their total height is 3 + 3 + 5 + 6 = 17. Any program can not stack the height of the tower 16, so the answer is 1.
#include <bits/stdc++.h>
using namespace std;
#define MAXN 0xfffff
int n,b,minh=MAXN,highh[MAXN];
void dfs(int sum,int high)
{
    if (high>=b)//判断奶牛的总高度大于书架的高度,则执行minh的赋值 
    {
        minh=min(minh,high);
        return;
    }
    if (sum>n)//如果一头奶牛的值已经大于了书架,则输出 
        return;
    dfs(sum+1,high);//增加牛的个数 
    dfs(sum+1,high+highh[sum]);//增加牛的高度 
}
int main()
{
    cin>>n>>b;
    for (int i=1;i<=n;i++)
        cin>>highh[i];
    dfs(1,0);
    cout<<minh-b;
    return 0;
}

Guess you like

Origin blog.csdn.net/Tangwan_jeff/article/details/95200883