日韩性视频-久久久蜜桃-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的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 日本草草影院 | 干夜夜 | 青青青手机视频在线观看 | 99色视频 | 爱爱视频免费网站 | 国内视频一区二区 | 成人网免费 | 激情黄色小视频 | 一级免费看 | 欧美精品一级二级三级 | 99这里只有 | 黑人巨大国产9丨视频 | 人人爱爱人人 | 精品一性一色一乱农村 | 日韩色小说 | 亚洲一区二区三区高清在线 | 香蕉在线视频播放 | 国产一在线观看 | 精品人妻一区二区三区换脸明星 | 国产视频久久久久久 | 中国女人内精69xxxxxx | 色多多黄色 | 曰韩一级片 | 黄色免费入口 | 久久久久人妻一道无码AV | 老司机免费在线视频 | 国产综合激情 | 在厨房拨开内裤进入毛片 | 91蝌蚪在线观看 | 三上悠亚在线一区 | 一区二区三区视频免费观看 | 91porn破解版 | 国产一精品一aⅴ一免费 | 天天天干 | 欧美在线视频一区二区 | 天天色影综合网 | 久久激情av | 免费人成在线 | 国产成人福利视频 | 国产ts在线视频 | 天天弄天天操 | 一色屋免费视频 | 黄色69视频 | 噼里啪啦国语版在线观看 | 五月婷婷深深爱 | 亚洲精久久 | jul023被夫上司连续侵犯 | 亚洲精品一区二区口爆 | 婷婷久久精品 | 日本一本视频 | 亚洲国产精品毛片 | 日本美女动态图 | 国产主播在线一区 | 婷婷开心激情 | 久久免费在线 | 国产一区二区视频免费在线观看 | 久久久久久久久久久国产 | 亚洲综合图片网 | 美女100%视频免费观看 | 色宗合 | 黄片毛片在线看 | 国产在线超碰 | 我会温柔一点的日剧 | 男女瑟瑟视频 | 91成人在线视频 | 青娱乐在线播放 | 亚洲精品视频在线观看免费 | 伊人网在线视频 | 麻豆午夜 | 亚洲视频第一页 | 91看片免费看 | 高潮av| 美女裸片| 欧美七区| 国产亚洲精品美女 | 日韩中文字幕精品视频 | 色婷婷午夜 | 特级西西www444人体聚色 | 一区二区三区国产在线观看 | 欧美xxx在线观看 | 一区二区三区精彩视频 | 操处女逼视频 | 久久综合久色欧美综合狠狠 | 日韩一区二区三区中文字幕 | 黄色a站| 在线播放www | 综合网五月 | 激情都市一区二区 | 欧美 国产 精品 | 日本韩国视频 | 韩日av一区二区 | 激情五月色播五月 | 国产91精品ai换脸 | 中文字幕亚洲成人 | 精品一区二区三区免费视频 | 人妻 日韩精品 中文字幕 | 日本特黄色片 | 久久久精品人妻一区二区三区色秀 | 国产高清精品一区二区三区 |