CodeForces
来源:互联网 发布:农村淘宝收费标准 编辑:程序博客网 时间:2024/05/16 18:50
CodeForces - 148E Porcelain (预处理,背包, dp)
【题目链接】http://codeforces.com/problemset/problem/148/E
题目内容
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.
Example
Input
2 33 3 7 23 4 1 5
Output
15
Input
1 34 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.
题目大意
公主不开心要摔瓷器,有n个架子,每个上面都放着瓷器,公主每次只能摔最边缘的一个,问m次以后最多能摔价值多大的瓷器~~
解题思路
把每排罐子处理成打碎i个的最大价值,可以先处理从左到右前j个罐子的总价值,两重for循环来做,然后就成了分组背包,相当于dp。
AC代码
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<string>#include<algorithm>#include<math.h>#include<limits.h>#include<stack>#include<queue>#define LL long long#define mod 1000000007using namespace std;int a[101][101];int b[101];//b[i] the amount of shelf i;int sumb[101];int sum[101];//sum[i] the first i goods's valueint c[101][101];//c[i][j] the most value for j goods int the i shelfint dp[101][10001]={0};// i shelf; j goods; max value;int main(){ int t,v,i,j,k; scanf("%d%d",&t,&v); memset(c,0,sizeof(c)); for(i=1;i<=t;i++) { memset(sum,0,sizeof(sum)); scanf("%d",&b[i]); sumb[i]=sumb[i-1]+b[i]; for(j=1;j<=b[i];j++) { scanf("%d",&a[i][j]); sum[j]=sum[j-1]+a[i][j]; } for(j=1;j<=b[i]&&j<=v;j++)//the number of goods ; for(k=0;k<=b[i]&&k<=j;k++)//the number of goods left; c[i][j]=max(sum[k]+sum[b[i]]-sum[b[i]-j+k],c[i][j]); } for(i=1;i<=t;i++) for(j=1;j<=v&&j<=sumb[i];j++) for(k=0;k<=b[i]&&k<=j;k++) dp[i][j]=max(dp[i-1][j-k]+c[i][k],dp[i][j]); printf("%d\n",dp[t][v]); return 0;}
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- 【管理学视频课程】精益生产的本质…
- 【经济学视频课程】科斯定理的本质…
- 【管理学视频课程】TOC制约理论的…
- 【管理学视频课程】关键链的本质--…
- 【军事学视频课程】《战争论》的基…
- CodeForces
- 定位四步骤和八方法--《可以量化的…
- Java Character 类
- 吉尔布雷斯的动作研究——《可以量化…
- 托夫勒的《权利的转移》--《可以量…
- Oracle Explain Plan
- windows7专业版64位 vmware虚拟机镜像,vmware打开直接能用
- 德鲁克理论—《可以量化的管理学》
- 哈默尔的核心竞争力--《可以量化的…