题目链接:http://codeforces.com/problemset/problem/148/E

E. Porcelain
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 on n 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 of m shrieks can inflict on the
collection of porcelain.

Input

The first line of input data contains two integers n (1 ≤ n ≤ 100)
and m (1 ≤ m ≤ 10000).
The next n lines contain the values of the items on the shelves: the first number gives the number of items on this shelf (an integer
between 1 and 100, inclusive),
followed by the values of the items (integers between 1 and 100,
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 least m.

Output

Output the maximal total value of a tantrum of m shrieks.

Examples
input
2 3
3 3 7 2
3 4 1 5
output
15
input
1 3
4 4 3 1 2
output
9
Note

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.


题解:

方法一:

1.对于每个书架,求出每种长度下的首尾相连的最大连续和。

2.通过类似解决背包问题的方法,求得在这些最大连续和的组合下的最大值,即为答案。

方法二:

反向思维:求两边的和最大,即求中间的和最小。

1.对于每个书架,求出每种长度下的最小连续和。

2.通过类似解决背包问题的方法,求得在这些最小连续和的组合下的最小值,然后再用总和减去这个最小值,即为答案。

写法:

i为第i个书架, j为dp到当前待组合的个数, k则为对于每一个书架相应的连续个数。

写法一:从小往大推,此时dp数组需要开二维。

写法二:从大往小推,此时dp数组只需开一维。

注意:写法二必须保证这一阶段的数据只能从上一阶段的数据转移过来, 而不能从同一阶段转移过来,否则会出现重复。

方法一写法一:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 1e4+; int n, m;
int dp[][maxn];
int a[][], sum[][], s[][]; void init()
{
scanf("%d%d",&n, &m);
for(int i = ; i<=n; i++)
{
scanf("%d",&a[i][]);
for(int j = ; j<=a[i][]; j++)
scanf("%d",&a[i][j]);
} for(int i = ; i<=n; i++) //求前缀和
for(int j = ; j<=a[i][]; j++)
sum[i][j] = sum[i][j-] + a[i][j]; for(int i = ; i<=n; i++) //求每个书架每种长度下,首尾相连序列的最大连续和
for(int l = ; l<=a[i][]; l++)
for(int r = ; l+r<=a[i][]; r++)
s[i][l+r] = max(s[i][l+r], sum[i][l] + sum[i][a[i][]] - sum[i][a[i][]-r]);
} void solve()
{
for(int i = ; i<=n; i++) //01背包的拓展,每个状态都从O(n)转移过来, 而普通背包则为O(1)
{
for(int j = ; j<=m; j++) //别忘了上一阶段的j状态也参与当前阶段j状态的转移
dp[i][j] = dp[i-][j]; for(int j = ; j<=m; j++)
for(int k = ; k<=min(j,a[i][]); k++)
dp[i][j] = max(dp[i][j], s[i][k]+dp[i-][j-k]);
}
cout<<dp[n][m]<<endl;
} int main()
{
init();
solve();
}

方法一写法二:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 1e4+; int n, m;
int dp[maxn];
int a[][], sum[][], s[][]; void init()
{
scanf("%d%d",&n, &m);
for(int i = ; i<=n; i++)
{
scanf("%d",&a[i][]);
for(int j = ; j<=a[i][]; j++)
scanf("%d",&a[i][j]);
} for(int i = ; i<=n; i++)
for(int j = ; j<=a[i][]; j++)
sum[i][j] = sum[i][j-] + a[i][j]; for(int i = ; i<=n; i++)
for(int l = ; l<=a[i][]; l++)
for(int r = ; l+r<=a[i][]; r++)
s[i][l+r] = max(s[i][l+r], sum[i][l] + sum[i][a[i][]] - sum[i][a[i][]-r]);
} void solve()
{
for(int i = ; i<=n; i++)
for(int j = m; j>=; j--)
for(int k = ; k<=min(j,a[i][]); k++)
dp[j] = max(dp[j], s[i][k]+dp[j-k]); cout<<dp[m]<<endl;
} int main()
{
init();
solve();
}

方法二写法一:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 1e4+; int n, m, all;
int dp[][maxn];
int a[][], sum[][], s[][]; void init()
{
scanf("%d%d",&n, &m);
m = -m;
for(int i = ; i<=n; i++)
{
scanf("%d",&a[i][]);
for(int j = ; j<=a[i][]; j++)
scanf("%d",&a[i][j]), all += a[i][j];
m += a[i][];
} for(int i = ; i<=n; i++) //前缀和
for(int j = ; j<=a[i][]; j++)
sum[i][j] = sum[i][j-] + a[i][j]; for(int i = ; i<=n; i++) //最小连续和,因为“最小”,所以需要初始化为最大
for(int j = ; j<=a[i][]; j++)
s[i][j] = INF; for(int i = ; i<=n; i++) //求最小连续和
for(int j = ; j<=a[i][]; j++)
for(int k = ; k<=j; k++)
s[i][j-k+] = min(s[i][j-k+], sum[i][j]-sum[i][k-]);
} void solve()
{
for(int i = ; i<=n; i++) //需要所有都初始化为最大
for(int j = ; j<=m; j++)
dp[i][j] = INF; for(int i = ; i<=n; i++)
{
for(int j = ; j<=m; j++)
dp[i][j] = dp[i-][j]; for(int j = ; j<=m; j++)
for(int k = ; k<=min(j,a[i][]); k++)
dp[i][j] = min(dp[i][j], s[i][k]+dp[i-][j-k]);
}
cout<<all - dp[n][m]<<endl;
} int main()
{
init();
solve();
}

方法二写法二:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 1e4+; int n, m, all;
int dp[maxn];
int a[][], sum[][], s[][]; void init()
{
scanf("%d%d",&n, &m);
m = -m;
for(int i = ; i<=n; i++)
{
scanf("%d",&a[i][]);
for(int j = ; j<=a[i][]; j++)
scanf("%d",&a[i][j]), all += a[i][j];
m += a[i][];
} for(int i = ; i<=n; i++)
for(int j = ; j<=a[i][]; j++)
sum[i][j] = sum[i][j-] + a[i][j]; for(int i = ; i<=n; i++)
for(int j = ; j<=a[i][]; j++)
s[i][j] = INF; for(int i = ; i<=n; i++)
for(int j = ; j<=a[i][]; j++)
for(int k = ; k<=j; k++)
s[i][j-k+] = min(s[i][j-k+], sum[i][j]-sum[i][k-]);
} void solve()
{
for(int j = ; j<=m; j++)
dp[j] = INF; for(int i = ; i<=n; i++)
for(int j = m; j>=; j--)
for(int k = ; k<=min(j,a[i][]); k++)
dp[j] = min(dp[j], s[i][k]+dp[j-k]); cout<<all-dp[m]<<endl;
} int main()
{
init();
solve();
}

最新文章

  1. UBER的故事
  2. java-并发-高级并发对象1
  3. C和指针 第十二章 使用结构和指针 双链表和语句提炼
  4. Linux系统下安装Mysql
  5. android4.x获取(也可监测)外置sd路径和读写
  6. javascript类的理解和使用
  7. php mysql_affected_rows获取sql执行影响的行数
  8. win7桌面便签。自带的
  9. HDU 4638 Group 树状数组 + 思路
  10. socket的一个错误的解释SocketException以及其他几个常见异常
  11. 使用JAVA对字符串进行DES加密解密(修正问题)
  12. ext4.0绘制chart(柱状图,条形图)
  13. jQuery.noConflict()防冲突机制
  14. 【C语言探索之旅】 第二部分第十课:练习题和习作
  15. Intellj IDEA 简易教程
  16. Android Material Design控件使用(四)——下拉刷新 SwipeRefreshLayout
  17. keras01 - hello world ~ 搭建第一个神经网络
  18. webpack4配置详解之常用插件分享
  19. js精度溢出解决方案
  20. onethink判断是否是手机访问?

热门文章

  1. RabbitMQ 最常用的三大模式
  2. Java集合——概述
  3. 转:关于使用ImageMagick和Tesseract进行简单数字图像识别
  4. javascript --- 兼容的那些事
  5. DVBS/S2在数字电视系统中的应用 三 (LNB介绍)
  6. 通过jstl判断是否给value 赋值
  7. vue2.0 自定义 下拉刷新和上拉加载更多(Scroller) 组件
  8. 机器学习(Machine Learning)&amp;amp;深度学习(Deep Learning)资料
  9. 第一个Hello,OS World操作系统
  10. cvpr2017年的所有论文下载