zoukankan      html  css  js  c++  java
  • Bookshelf 2(poj 3628)

    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 has N cows (1 ≤ N ≤ 20) each with some height of Hi (1 ≤ Hi ≤ 1,000,000 - these are very tall cows). The bookshelf has a height of B (1 ≤ B ≤ S, where S is 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 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 16
    3
    1
    3
    5
    6

    Sample Output

    1

    题目大意 : 有N头牛一个书架,书架太高要站在牛身上够书架,已知N头牛的高度和书架的高度,一头牛可以站在另一头牛身上(牛顿的棺材板在晃动),总高度是他们的高度之和,高度和需要不小于(大于等于均可)书架高度,问符合要求的最低高度是多少,输出该高度与书架高度的差值。

    解析 : 能够穷举出所有高度,然后遍历判断一遍符合条件的值就得解了,刚好这题数据不大20,可以暴力枚举的,代码我就不贴出来了。最近在学习dp,这道题可以变形为01背包问题,dp储存牛的高度和,背包容量 v 为所有牛的高度总和,价值和体积就是每头牛的高度。

     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>
    10 #include<algorithm>
    11 using namespace std;
    12 #define oo 0x3f3f3f3f
    13 #define maxn 1000010
    14 
    15 int dp[maxn],val[30];
    16 
    17 int main()
    18 {
    19     int n,k;
    20     while(scanf("%d%d",&n,&k)!=EOF)
    21     {
    22         int sum=0;
    23         for(int i=1; i<=n; i++)
    24         {
    25             scanf("%d",&val[i]);
    26             sum+=val[i];
    27         }
    28         int Min=oo;
    29         for(int i=1; i<=n; i++)
    30             for(int j=sum; j>=val[i]; j--)
    31             {
    32                 dp[j]=max(dp[j],dp[j-val[i]]+val[i]);
    33                 if(dp[j]>=k)//这里是>=,英语不好,只> w了一次
    34                 {
    35                     Min=min(Min,dp[j]);
    36                 }
    37             }
    38         printf("%d
    ",Min-k);
    39     }
    40     return 0;
    41 }
    在网上看巨巨们的帖子,发现还可以用搜索来写

     1 #include<cstdio>  
     2 #include<cstring>  
     3 #include<iostream>  
     4 using namespace std;  
     5 int h[22], ans, flag;  
     6 int n, m;  
     7 void dfs(int k, int s)  
     8 {  
     9     if(s == m)  
    10     {  
    11         ans = 0;  
    12         return ;  
    13     }  
    14     if(s >= m)  
    15     {  
    16         if(s - m < ans)  
    17             ans = s - m;  
    18         return ;  
    19     }  
    20     for(int i = k; i < n; i++)  
    21     {  
    22         dfs(i+1,s+h[i]);  
    23     }  
    24 }  
    25 int main()  
    26 {  
    27     int i;  
    28     while(cin >> n >> m)  
    29     {  
    30         int sum = 0;  
    31         flag = 0;  
    32         for(i = 0; i < n; i++)  
    33         {  
    34             cin >> h[i];  
    35             sum += h[i];  
    36         }  
    37         if(sum == m)  
    38         {  
    39             cout << "0" << endl;  
    40             continue;  
    41         }  
    42         ans = sum;  
    43         dfs(0,0);  
    44         cout << ans << endl;  
    45     }  
    46     return 0;  
    47 }  
    该代码转自 http://blog.csdn.net/lyhvoyage/article/details/17042369


  • 相关阅读:
    数据结构之栈和队列
    数据结构之线性表
    关于优惠券优惠的思路以及实践
    mysql基础知识扫盲
    rabbitMQ第五篇:Spring集成RabbitMQ
    rabbitMQ第四篇:远程调用
    rabbitMQ第三篇:采用不同的交换机规则
    rabbitMQ第二篇:java简单的实现RabbitMQ
    rabbitMQ第一篇:rabbitMQ的安装和配置
    java容器详细解析
  • 原文地址:https://www.cnblogs.com/zz1164056454/p/5735379.html
Copyright © 2011-2022 走看看