写到要疯掉,这种需要细节的题目得在精神状态比较好的时候写。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#define mem(a,x) memset(a,x,sizeof(a))
#define s1(x) scanf("%d",&x)
#define s2(x,y) scanf("%d%d",&x,&y)
#define s3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define s4(x,y,z,k) scanf("%d%d%d%d",&x,&y,&z,&k)
#define ff(a,n) for(int i = 0 ; i < n; i++) scanf("%d",a+i)
#define ls 2*rt
#define rs 2*rt+1
#define lson ls,L,mid
#define rson rs,mid+1,R
#define ll long long
using namespace std;
typedef pair<int,int> pii;
//inline ll ask(int x){ll res=0;while(x)res+=c[x],x-=x&(-x);return res;}
//inline void add(int x,int d){while(x<=n)c[x]+=d,x+=x&(-x);}
//int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b);}
const ll inf = 0x3f3f3f3f;
const int mx = 160;
int up[mx],le[mx],ri[mx],low[mx];
int ma[mx][mx],f[mx];
int n,m,p,x1,x2,y2,y3,now;void calc(){
// up[0] = low[n + 1] = le[0] = ri[m + 1] = -inf;mem(up,-inf); //忘了初始化 mem(low,-inf);mem(le,-inf);mem(ri,-inf);now = -inf;for(int i = 1; i <= n; i++){ //上边 mem(f,0);for(int j = i; j <= n; j++){for(int k = 1; k <= m; k++)f[k] += ma[j][k];up[j] = max(up[j-1],up[j]);int sum = 0,s;for(int k = 1; k <= m ;k++){if(sum <= 0){sum = f[k];s = k;}elsesum += f[k]; if(sum > now){now = sum;x1 = i;x2=j;y2=s;y3=k;}up[j]=max(up[j],sum);}}}for(int i = n; i >= 1; i--){ // 下面 mem(f,0);for(int j = i; j >= 1; j--){for(int k = 1; k <= m; k++)f[k] += ma[j][k];low[j] = max(low[j+1],low[j]);int sum = 0;for(int k = 1; k <= m ;k++){if(sum <= 0){sum = f[k];}elsesum += f[k]; low[j]=max(low[j],sum);}}}for(int i = 1; i <= m; i++){ //左边 mem(f,0);for(int j = i; j <= m; j++){for(int k = 1; k <= n; k++)f[k] += ma[k][j];le[j] = max(le[j-1],le[j]);int sum = 0;for(int k = 1; k <= n ;k++){if(sum <= 0){sum = f[k];}elsesum += f[k]; le[j]=max(le[j],sum);}}}for(int i = m; i >= 1; i--){ //右边 mem(f,0);for(int j = i; j >= 1; j--){for(int k = 1; k <= n; k++)f[k] += ma[k][j];ri[j] = max(ri[j+1],ri[j]);int sum = 0;for(int k = 1; k <= n ;k++){if(sum <= 0){sum = f[k];}elsesum += f[k]; ri[j]=max(ri[j],sum);}}}int temp = now;for(int i = x1; i <= x2; i++)for(int j = y2; j <= y3; j++){if(p-ma[i][j] >= 0)continue;int m1 = temp-ma[i][j]+p;if(j-1>=1) m1 = max(m1,le[j-1]);if(j+1<=m) m1 = max(m1,ri[j+1]); //m错写n if(i-1>=1) m1 = max(m1,up[i-1]); //两个函数写反了 if(i+1<=n) m1 = max(m1,low[i+1]);now = min(m1,now);}printf("%d\n",now);}
int main(){// freopen("F:\\in.txt","r",stdin);//int T=10; scanf("%d",&T);while(s3(n,m,p) != EOF){for(int i = 1; i <= n; i++)for(int j = 1; j <= m; j++)s1(ma[i][j]);calc();} return 0;
}