题目链接: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();
}

Codeforces Round #105 (Div. 2) E. Porcelain —— DP(背包问题)的更多相关文章

  1. 水题 Codeforces Round #105 (Div. 2) B. Escape

    题目传送门 /* 水题:这题唯一要注意的是要用double,princess可能在一个小时之内被dragon赶上 */ #include <cstdio> #include <alg ...

  2. Codeforces Round #105 (Div. 2) D. Bag of mice 概率dp

    题目链接: http://codeforces.com/problemset/problem/148/D D. Bag of mice time limit per test2 secondsmemo ...

  3. Codeforces Round #260 (Div. 1) A - Boredom DP

    A. Boredom Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/problem/A ...

  4. Codeforces Round #131 (Div. 1) B. Numbers dp

    题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...

  5. Codeforces Round #131 (Div. 2) B. Hometask dp

    题目链接: http://codeforces.com/problemset/problem/214/B Hometask time limit per test:2 secondsmemory li ...

  6. Codeforces Round #276 (Div. 1) D. Kindergarten dp

    D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...

  7. Codeforces Round #105 (Div. 2) ABCDE

    A. Insomnia cure 哎 只能说英语太差,一眼题我看了三分钟. 题意:给5个数k, l, m, n 和 d,求1~d中能被k, l, m, n 至少一个整除的数的个数. 题解:…… 代码: ...

  8. Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS

    题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...

  9. Codeforces Round #539 (Div. 2) 异或 + dp

    https://codeforces.com/contest/1113/problem/C 题意 一个n个数字的数组a[],求有多少对l,r满足\(sum[l,mid]=sum[mid+1,r]\), ...

随机推荐

  1. SSL剥离工具sslstrip

    SSL剥离工具sslstrip   在日常上网过程中,用户只是在地址栏中输入网站域名,而不添加协议类型,如HTTP和HTTPS.这时,浏览器会默认在域名之前添加http://,然后请求网站.如果网站采 ...

  2. Codeforces Gym 100203G Good elements 暴力乱搞

    原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑暴力的复杂度是O(n^3),所以 ...

  3. Java多线程中Lock的使用

    Jdk1.5以后,在java.util.concurrent.locks包下,有一组实现线程同步的接口和类,说到线程的同步,可能大家都会想到synchronized关键字, 这是java内置的关键字, ...

  4. xamarin studio 中SpinButton ComBox Splid 鼠标放上去就会自动接收焦点,然后进行数值变化

    公司做跨平台项目,用XamarinStudio 开发mac版本,语法还是C#,但是,尼玛XamarinStudio的控件就是坑爹啊. 其他的暂时不累赘,笔者画界面,一堆控件放到一个界面上,当超出屏幕时 ...

  5. mac apache配置虚拟主机

    设置虚拟主机 在终端运行“sudo vi /etc/apache2/httpd.conf”,打开Apche的配置文件 在httpd.conf中找到“#Include /private/etc/apac ...

  6. Wish3D用户必看!模型加载失败原因汇总

    上传到Wish3D的模型加载不出来,作品显示页面漆黑一片,是什么原因? 很有可能是操作过程中的小失误,不妨从以下几点检查.还是不行的请加QQ群(Wish3D交流群3):635725654,@Wish3 ...

  7. QlikView显示所选时间前一年的数据

    客户常常提出这种需求,当用户选择某一时间时.图表中显示所选时间之前一年的数据.以下是我的方法.如有不当,请多不吝赐教: 数据准备例如以下所看到的: SalesData: LOAD Num(ID) as ...

  8. Swagger学习和实践

    Swagger学习和实践 学习了:https://www.cnblogs.com/zxtceq/p/5530396.html swagger 英 [ˈswægə(r)] 美 [ˈswæɡɚ] vi.昂 ...

  9. Triangle 三角形——找出三角形中从上至下和最小的路

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  10. C++卷积神经网络实例:tiny_cnn代码具体解释(7)——fully_connected_layer层结构类分析

    之前的博文中已经将卷积层.下採样层进行了分析.在这篇博文中我们对最后一个顶层层结构fully_connected_layer类(全连接层)进行分析: 一.卷积神经网路中的全连接层 在卷积神经网络中全连 ...