您的位置:首页 > 其它

[预处理优化][动态规划]D

2014-04-29 13:02 495 查看

Problem D: D

Time Limit: 5 Sec  Memory Limit:
128 MB
Submit: 64  Solved: 9

[Submit][Status][Web
Board]

Description

There is a N*M matrix, and each cell of the matrix has an integer number. A man can entry the matrix at each cell of the first row, then he can stop at any position. He can go down,left
or right, but not outside the matrix. When he stops, the sum of numbers in the visited cell is his score. He can visit a cell more than once, but the number in this cell is still counted once.
[align=left]Please help he get the maximum score.[/align]

Input

[align=left]The first line contains a integer number T indicates the number of test case.[/align]
 The first line of each test case has two integer:N M. Next N lines describe the N*M matrix,each line has M integers in range [-10^9,10^9].
(1<=N<=1000,1<=M<=50)

Output

[align=left]For each test case, print the max score in a line.[/align]

Sample Input

33 31 1 11 1 11 1 13 3-1 -1 -1-1 -1 -1-1 -1 -13 31 1 1-1 -1 -11 1 1

Sample Output

9-15

HINT

由于只能向左右下移动,因此每一行仅覆盖一个连续区间,且相邻两行的区间必定有交集。

设f[i,j]表示第i行覆盖到j。f[i,j]如何从f[i-1,k]转移过来?

容易发现,第i-1行的[j,k]区间是必须要取的。而[1,j-1]则需要取一个和最大的后缀,[k+1,m]取一个和最大的前缀。

转移方程即为f[i,j] = max(f[i-1,k] + sum[k-1]-sum[j]+maxl[j]+maxr[k]),其中j<k

                                           f[i-1,k] + num[k],其中j==k

                                   max(f[i-1,k] + sum[j-1]-sum[k]+maxl[k]+maxr[j]),其中j>k

(由于我的maxl和maxr是闭区间的,所以需要这样写)

必须取的我们预处理出sum前缀即可,后两者用O(n*m^2)的动态规划预处理即可,可以继续优化到O(n*m),但是对于本题已经足够优。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long ll;

ll f[1010][60];
ll sum[1010][60];
int num[1010][60];
ll maxl[1010][60];
ll maxr[1010][60];

const ll inf = 0x3f3f3f3f3f3f3f3fll;

int main()
{
int t;
scanf("%d",&t);
while (t--)
{
memset(f,0,sizeof f);
int n,m;
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
{
maxl[i][0] = maxr[i][m+1] = 0;
for (int j=1;j<=m;j++)
{
scanf("%d",&num[i][j]);
sum[i][j] = sum[i][j-1] + (ll)num[i][j];
maxl[i][j] = -inf;
for (int k=0;k<j;k++)
{
maxl[i][j] = max(maxl[i][j],sum[i][j]-sum[i][k]);
}
}
for (int j=1;j<=m;j++)
{
maxr[i][j] = -inf;
for (int k=m;k>=j;k--)
{
maxr[i][j] = max(maxr[i][j],sum[i][k]-sum[i][j-1]);
}
}
}

ll ans = -inf;
/*
for (int i=1;i<=m;i++)
{
f[1][i] = maxl[1][i-1]+maxr[1][i];
ans = max(ans,f[1][i]);
}
*/
memset(f[1],0,sizeof f[1]);
for (int i=2;i<=n+1;i++)
{
for (int p=1;p<=m;p++)
{
f[i][p] = -inf;
for (int q=1;q<=m;q++)
{
if (p < q)
f[i][p] = max(f[i][p], f[i-1][q]+maxl[i-1][p]+maxr[i-1][q]+sum[i-1][q-1]-sum[i-1][p]);
else if (p > q)
f[i][p] = max(f[i][p], f[i-1][q]+maxl[i-1][q]+maxr[i-1][p]+sum[i-1][p-1]-sum[i-1][q]);
else if (p == q)
f[i][p] = max(f[i][p], f[i-1][q]+maxl[i-1][q]+maxr[i-1][p]-num[i-1][p]);
ans = max(ans,f[i][p]);
}
}
}
cout << ans << endl;
}
return 0;
}

/**************************************************************
Problem: 1268
User: ssdut_team06
Language: C++
Result: Accepted
Time:392 ms
Memory:3780 kb
****************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: