标签:lib stream ios lang ble printf cst 最大和 fine
分情况
/************************************************************** Problem: 1084 User: lxy8584099 Language: C++ Result: Accepted Time:84 ms Memory:2004 kb ****************************************************************/ /* m==1 f[i][j]表示前i个数中取出j个矩形的最大和 m==2 w[i][j][k]表示第一列选到第i个数,第二列选到第j个数时,总共k个子矩形的答案 */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; #define MAXN 110 #define INF 32768 int n,m,K,a; int f[MAXN][15],w[MAXN][MAXN][15],s[MAXN],sum[MAXN][2]; int main() { scanf("%d%d%d", &n, &m, &K); if (m==1) { for (int i=1;i<=n;i++) { scanf("%d", &a); s[i]=s[i-1]+a; } for (int i=0;i<=n;i++) for (int j=1;j<=K;j++) f[i][j]=-INF; for (int i=1;i<=n;i++) for (int j=1;j<=K;j++) { f[i][j]=f[i-1][j]; for (int i1=0;i1<i;i1++) f[i][j]=max(f[i][j],f[i1][j-1]+s[i]-s[i1]); } printf("%d", f[n][K]); } else { for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) { scanf("%d", &a); sum[i][j]=sum[i-1][j]+a; } for (int i=0;i<=n;i++) for (int j=0;j<=n;j++) for (int k=1;k<=K;k++) w[i][j][k]=-INF; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) for (int k=1;k<=K;k++) { w[i][j][k]=max(w[i-1][j][k],w[i][j-1][k]); for (int i1=0;i1<i;i1++) w[i][j][k]=max(w[i1][j][k-1]+sum[i][1]-sum[i1][1],w[i][j][k]); for (int j1=0;j1<j;j1++) w[i][j][k]=max(w[i][j1][k-1]+sum[j][2]-sum[j1][2],w[i][j][k]); if (i==j) { for (int i1=0;i1<i;i1++) w[i][i][k]=max(w[i][i][k],w[i1][i1][k-1]+sum[i][1]+sum[i][2]-sum[i1][1]-sum[i1][2]); } } printf("%d", w[n][n][K]); } }
标签:lib stream ios lang ble printf cst 最大和 fine
原文地址:https://www.cnblogs.com/lxy8584099/p/10318822.html