Codeforces 148E Porcelain (預處理+多重揹包)
During her tantrums the princess usually smashes some collectable porcelain. Every furious shriek is accompanied with one item smashed.
The collection of porcelain is arranged neatly onn shelves. Within each shelf the items are placed in one row, so that one can access only the outermost items — the leftmost or the rightmost item, not the ones in the middle of the shelf. Once an item is taken, the next item on that side of the shelf can be accessed (see example). Once an item is taken, it can't be returned to the shelves.
You are given the values of all items. Your task is to find the maximal damage the princess' tantrum ofm shrieks can inflict on the collection of porcelain.
The first line of input data contains two integersn (1 ≤ n ≤ 100) andm (1 ≤ m ≤ 10000). The nextn lines contain the values of the items on the shelves: the first number gives the number of items on this shelf (an integer between1 and100, inclusive), followed by the values of the items (integers between1 and100, inclusive), in the order in which they appear on the shelf (the first number corresponds to the leftmost item, the last one — to the rightmost one). The total number of items is guaranteed to be at leastm.
Output the maximal total value of a tantrum of m shrieks.
2 3
3 3 7 2
3 4 1 5
15
1 3
4 4 3 1 2
9
In the first case there are two shelves, each with three items. To maximize the total value of the items chosen, one can take two items from the left side of the first shelf and one item from the right side of the second shelf.
In the second case there is only one shelf, so all three items are taken from it — two from the left side and one from the right side.
題目連結:http://codeforces.com/contest/148/problem/e
題目大意:給n層數字,一共要取m次,每次取數字只能從任意一層的最左或最右取
題目分析:預處理出每層從兩邊取j (j <= m)個的所有可能的最大值,之後就是個普通的多重揹包問題
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int sum[105], ma[105], dp[10005];
int main()
{
int n, m;
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++)
{
int num;
scanf("%d", &num);
for(int j = 1; j <= num; j++)
{
int tmp;
scanf("%d", &tmp);
sum[j] = sum[j - 1] + tmp; //計算字首和
}
//預處理
memset(ma, 0, sizeof(ma));
for(int j = 0; j <= num; j++)
for(int k = 0; k <= j; k++)
ma[j] = max(ma[j], sum[k] + sum[num] - sum[num + k - j]);
printf("\n");
//多重揹包
for(int j = m; j >= 1; j--)
for(int k = 1; k <= min(j, num); k ++)
dp[j] = max(dp[j], dp[j - k] + ma[k]);
}
printf("%d\n", dp[m]);
}
相關文章
- 01揹包、完全揹包、多重揹包詳解
- javascript演算法基礎之01揹包,完全揹包,多重揹包實現JavaScript演算法
- 【BZOJ 5003】與鏈 (多重揹包 dp)
- 多重揹包二進位制分解思想講解
- hihocoder 1285 智力競賽 (類多重揹包)
- 多重揹包動態規劃及空間優化動態規劃優化
- 多重揹包問題的單調佇列優化佇列優化
- POJ1276Cash Machine[多重揹包可行性]Mac
- Codeforces 366C Dima and Salad:揹包dp
- 揹包問題(01揹包與完全揹包)
- 揹包DP——完全揹包
- 揹包DP——混合揹包
- 雙核處理(動態規劃的01揹包問題)動態規劃
- 分組揹包、完全揹包
- Codeforces Round #214 (Div. 2)(揹包變形)
- NOIP2012pj擺花[DP 多重揹包方案數]
- 揹包
- 01揹包、有依賴的揹包
- poj1276 多重揹包問題(二進位制解決方案)
- 多重揹包O(N*V)演算法詳解(使用單調佇列)演算法佇列
- Codeforces Round #360 (Div. 2) E dp 類似01揹包
- codevs 3372 選學霸(hash+並查集+多重揹包)dev並查集
- 01 揹包
- 揹包DP
- 揹包問題
- 程式碼隨想錄演算法訓練營第四十六天| 139.單詞拆分 多重揹包 揹包問題總結篇!演算法
- 影像預處理
- 預處理指令
- 預處理命令
- ACM 揹包問題ACM
- 01揹包問題
- dp-完全揹包
- 通天之分組揹包
- 資料預處理
- 影像預處理方法
- 01 揹包的變形
- 揹包問題大合集
- 揹包題型總結