日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Bookshelf 2

發布時間:2024/10/5 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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; }

?

總結

以上是生活随笔為你收集整理的Bookshelf 2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 午夜av电影在线观看 | 天天摸天天看 | 国产一级片免费视频 | 永久视频在线观看 | 日韩制服诱惑 | 精品国产视频 | 午夜精品成人 | 淫欲av| 欧美 日本 国产 | 免费在线 | 午夜激情在线 | 国产精品日日做人人爱 | 国产精品伦理一区 | 欧美精品成人一区二区三区四区 | 精品人妻一区二区色欲产成人 | 最近日韩中文字幕中文 | 日本免费a级片 | 西西4444www大胆无码 | 草草影院国产第一页 | 毛片999| 午夜三区| 交专区videossex非洲 | 岛国久久久 | 一区二区三区不卡在线 | 韩国日本中文字幕 | 日本亚洲欧美在线 | 亚洲午夜无码久久久久 | 成人无码精品1区2区3区免费看 | 欧美韩国日本在线 | 欧美日韩三级视频 | 中国免费毛片 | www.亚洲色图.com | 日本男人天堂 | 欧洲精品久久久久毛片完整版 | 久久久999成人| 欧美一级影院 | 看91 | 国产一区二区三区四区三区四 | 特一级黄色大片 | 国产黄a三级三级看三级 | 亚洲图区综合 | 亚洲成人一区 | 久操热 | 久插视频 | 日本激情视频在线观看 | 毛片av在线播放 | 亚洲最新av网站 | 天堂а√在线中文在线鲁大师 | 三级毛毛片 | 日韩专区中文字幕 | 国产毛片a | 亚洲无吗在线观看 | av无码av天天av天天爽 | 国产精品亚洲成在人线 | 国产精品永久免费 | 亚洲激情国产 | 亚洲成在线| 99久99| 91污在线观看 | 茄子视频懂你更多在线观看 | 高清欧美精品xxxxx在线看 | 超碰一区二区三区 | 欧美xxxxbbb | 青青草综合 | 秋霞黄色网| 啪啪激情网 | 国产区在线观看视频 | 69精品视频| 秋霞成人午夜鲁丝一区二区三区 | 亚洲人无码成www久久 | 中文字幕――色哟哟 | 欧美日韩二区三区 | mm1313亚洲国产精品美女 | 在线不卡一区 | 宝贝乖h调教灌尿穿环 | 日韩一区二区免费在线观看 | 久久国产精品久久久久久电车 | 99国产精品视频免费观看一公开 | 成年人在线观看视频 | 人与禽性7777777 | 久久视频这里只有精品 | 调教丰满的已婚少妇在线观看 | 国产黄色录像片 | 中文字幕乱码无码人妻系列蜜桃 | 99久久久国产精品无码网爆 | 国产一区二区激情 | 国产在线视频网址 | 精品一区二区三区免费看 | 亚洲熟妇一区二区三区 | 欧美亚洲精品一区二区 | 金8天国av| 色综合中文 | 高潮白浆女日韩av免费看 | 亚洲私人影院 | 禁止18在线观看 | 亚洲激情另类 | 精品成人无码一区二区三区 | 丰满少妇一级片 | a三级黄色片|