CF E. Porcelain (双向dp)
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.
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 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.
题意:给n层数字,总共要取m次,每次只能取一层的两端,求最大价值
思路:预处理+01背包
F【i】【j】表示第i层取了j次的最大代价
p【i】表示取了i次的最大价值。
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
int n,M,m[10100],f[10100][110],a[10100],ans=0;
int sum[10100],p1[20100],p2[20100],p[20010];
int read()
{
char c=getchar();int ans=0;
while(c>'9'||c<'0') c=getchar();
while(c<='9'&&c>='0') ans=ans*10+c-'0',c=getchar();
return ans;
}
int main()
{
n=read(); M=read();
for(int k=1;k<=n;k++)
{
m[k]=read();
for(int i=1;i<=m[k];i++) a[i]=read(),a[i]+=a[i-1];
f[k][0]=0;
for(int i=0;i<=m[k];i++)
{
for(int j=0;j+i<=m[k];j++)
f[k][m[k]-i]=max(a[m[k]]-(a[j+i]-a[j]),f[k][m[k]-i]);
}
}
for(int i=1;i<=n;i++)
for(int j=M;j>=1;j--)
for(int k=1;k<=min(j,m[i]);k++)
p[j]=max(p[j-k]+f[i][k],p[j]);
printf("%d\n",p[M]);
}
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
int n,M,m[10100],f[10100][110],a[10100],ans=0;
int sum[10100],p1[20100],p2[20100];
int read()
{
char c=getchar();int ans=0;
while(c>'9'||c<'0') c=getchar();
while(c<='9'&&c>='0') ans=ans*10+c-'0',c=getchar();
return ans;
}
int main()
{
memset(p1,0,sizeof(p1));
memset(p2,0,sizeof(p2));
n=read(); M=read();
for(int k=1;k<=n;k++)
{
m[k]=read();
for(int i=1;i<=m[k];i++) a[i]=read(),a[i]+=a[i-1];
f[k][0]=0;
for(int i=0;i<=m[k];i++)
{
for(int j=0;j+i<=m[k];j++)
f[k][m[k]-i]=max(a[m[k]]-(a[j+i]-a[j]),f[k][m[k]-i]);
}
}
// for(int i=1;i<=n;i++)
// {
// for(int j=1;j<=m[i];j++) printf("%d ",f[i][j]);
// printf("\n");
// }
for(int i=1;i<=n/2;i++) sum[i]=sum[i-1]+m[i];
if(n>=2)for(int i=1;i<=m[1];i++) p1[i]=f[1][i];
for(int i=2;i<=n/2;i++)
for(int j=min(sum[i],M);j>=1;j--)
for(int k=1;k<=min(m[i],j);k++)
p1[j]=max(p1[j],p1[j-k]+f[i][k]);
// for(int i=1;i<=sum[n/2];i++) printf("%d ",p1[i]);
// printf("\n");
sum[n/2]=0;
for(int i=n/2+1;i<=n;i++) sum[i]=sum[i-1]+m[i];
for(int i=n/2+1;i<=n;i++)
for(int j=min(sum[i],M);j>=1;j--)
for(int k=1;k<=min(j,m[i]);k++)
p2[j]=max(p2[j],p2[j-k]+f[i][k]);
// for(int i=1;i<=sum[n];i++) printf("%d ",p2[i]);
// printf("\n");
for(int i=1;i<=M;i++) p2[i]=max(p2[i],p2[i-1]);
for(int i=1;i<=M;i++) p1[i]=max(p1[i],p1[i-1]);
// for(int i=1;i<=M;i++) printf("%d ",p1[i]);
// printf("\n");
for(int i=1;i<=min(sum[n],M);i++)
ans=max(p2[i]+p1[M-i],ans);
for(int i=1;i<=min(sum[n/2-1]+m[n/2],M);i++)
ans=max(p1[i]+p2[M-i],ans);
printf("%d\n",ans);
}