[SCOI2005][BZOJ1084] 最大子矩阵

1084: [SCOI2005]最大子矩阵

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1533  Solved: 773
[Submit][Status][Discuss]

3 2 2
1 -3
2 3
-2 3

Sample Output

9

1<=m<=2。。。。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n,m,kk,a[1001],b[1001],s[1001],s1[1001],s2[1001],dp[1001][11],f[1001][1001][11];
int main()
{
scanf("%d%d%d",&n,&m,&kk);
if (m==1)
{
for (int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
s[i]=s[i-1]+a[i];
}
for (int i=1;i<=n;i++)
for (int k=1;k<=kk;k++)
{
dp[i][k]=dp[i-1][k];
for (int j=0;j<i;j++)
dp[i][k]=max(dp[i][k],dp[j][k-1]+s[i]-s[j]);
}
printf("%d",dp[n][kk]);
}
if (m==2)
{
for (int i=1;i<=n;i++)
{
scanf("%d%d",&a[i],&b[i]);
s1[i]=s1[i-1]+a[i];
s2[i]=s2[i-1]+b[i];
}
for (int k=1;k<=kk;k++)
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
{
f[i][j][k]=max(f[i-1][j][k],f[i][j-1][k]);
for (int l=0;l<i;l++) f[i][j][k]=max(f[i][j][k],f[l][j][k-1]+s1[i]-s1[l]);
for (int l=0;l<j;l++) f[i][j][k]=max(f[i][j][k],f[i][l][k-1]+s2[j]-s2[l]);
if (i==j)
for (int l=0;l<i;l++) f[i][j][k]=max(f[i][j][k],f[l][l][k-1]+s1[i]-s1[l]+s2[j]-s2[l]);
}
printf("%d",f[n][n][kk]);
}
return 0;
}

posted @ 2015-07-13 16:16  ws_fqk  阅读(117)  评论(0编辑  收藏