Bookshelf 2

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

C++版本一

DP 

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int n,m;
int c[1100],dp[1001000 ];
int main()
{
        scanf("%d%d",&n,&m);
        int sum=0;
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++){
            scanf("%d",&c[i]);
            sum+=c[i];
        }

        sort(c,c+n);

        for(int i=0;i<n;i++){
            for(int k=sum;k>=c[i];k--){
                dp[k]=max(dp[k],dp[k-c[i]]+c[i]);
            }
        }
        for(int i=m;i<=sum;i++){
            if(dp[i]>=m){
               cout<< dp[i]-m <<endl;
               break;
            }
        }


    //cout << "Hello world!" << endl;
    return 0;
}

 C++版本二

DP

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[2000002], h[22];
int main()
{
    int n, m, i, j;
    while(~scanf("%d%d",&n,&m))
    {
        int sum = 0;
        memset(dp,0,sizeof(dp));
        for(i = 1; i <= n; i++)
        {
            scanf("%d",&h[i]);
            sum += h[i];
        }
        for(i = 1; i <= n; i++)
            for(j = sum; j >= h[i]; j--)
                dp[j] = max(dp[j], dp[j - h[i]] + h[i]);
        int Min = sum;
        for(i = m; i <= sum; i++)
            if(dp[i] >= m && dp[i] - m < Min)
                Min = dp[i] - m;
        printf("%d\n",Min);
    }
    return 0;
}

C++版本三

DFS 

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int h[22], ans, flag;
int n, m;
void dfs(int k, int s)
{
	if(s == m)
	{
		ans = 0;
		return ;
	}
	if(s >= m)
	{
		if(s - m < ans)
			ans = s - m;
		return ;
	}
	for(int i = k; i < n; i++)
	{
		dfs(i+1,s+h[i]);
	}
}
int main()
{
	int i;
	while(cin >> n >> m)
	{
		int sum = 0;
		flag = 0;
		for(i = 0; i < n; i++)
		{
			cin >> h[i];
			sum += h[i];
		}
		if(sum == m)
		{
			cout << "0" << endl;
			continue;
		}
		ans = sum;
		dfs(0,0);
		cout << ans << endl;
	}
	return 0;
}

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-04 15:20
牛客61197583...:看到室友一个个没怎么学通过关系直接入职或者接到面试,真的很难受。八股不知道背了多少遍,hot100也刷了1.5遍了,但就是没有面试的机会,唉
点赞 评论 收藏
分享
06-13 17:33
门头沟学院 Java
顺序不记了,大致顺序是这样的,有的相同知识点写分开了1.基本数据类型2.基本数据类型和包装类型的区别3.==和equals区别4.ArrayList与LinkedList区别5.hashmap底层原理,put操作时会发生什么6.说出几种树型数据结构7.B树和B+树区别8.jvm加载类机制9.线程池核心参数10.创建线程池的几种方式11.callable与runnable区别12.线程池怎么回收线程13.redis三剑客14.布隆过滤器原理,不要背八股,说说真正使用时遇到了问题没有(我说没有,不知道该怎么回答了)15.堆的内存结构16.自己在写项目时有没有遇见过oom,如何处理,不要背八股,根据真实经验,我说不会17.redis死锁怎么办,watchdog机制如何发现是否锁过期18.如何避免redis红锁19.一个表性别与年龄如何加索引20.自己的项目的QPS怎么测的,有没有真正遇到大数量表21.说一说泛型22.springboot自动装配原理23.springmvc与springboot区别24.aop使用过嘛?动态代理与静态代理区别25.spring循环依赖怎么解决26.你说用过es,es如何分片,怎么存的数据,1000万条数据怎么写入库中27.你说用limit,那么在数据量大之后,如何优化28.rabbitmq如何批次发送,批量读取,答了延迟队列和线程池,都不对29.计网知不知道smtp协议,不知道写了对不对,完全听懵了30.springcloud知道嘛?只是了解反问1.做什么的?短信服务,信息量能到千万级2.对我的建议,基础不错,但是不要只背八股,多去实际开发中理解。面试官人不错,虽然没露脸,但是中间会引导我回答问题,不会的也只是说对我要求没那么高。面完问我在济宁生活有没有困难,最快什么时候到,让人事给我聊薪资了。下午人事打电话,问我27届的会不会跑路,还在想办法如何使我不跑路,不想扣我薪资等。之后我再联系吧,还挺想去的😭,我真不跑路哥😢附一张河科大幽默大专图,科大就是大专罢了
查看30道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务