-
3 3 4 -100 4 4 4 -10 4 4 4 4 3 3 -1 -2 -2 -2 -2 -2 -2 -2 -2 -2
样例输出 -
24
-1
-
-
给你 nmk,让你把矩阵的一个数替换成 k,必须换一个且只换一个,那么就是最大子矩阵,然后先用rmq处理一下最小值,就可以,直接替换了最小值了,在这里还有再加一个值,就是以前已经替换过了的 sum1,sum没什么好说的,就是常规的最大子矩阵的套路,sum1可以因为可以随时替换变量,所以是一直更新的,sum1可以从是 max(sum+替换这次的结果,以前替换的结果+k,替换这次的结果),
-
-
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; int zhi[310][310]; int n,m; int lg[310]; int st[310][310][20]; inline int rmq(int id,int l,int r){ if(l>r) return 0; int k=lg[r-l+1]; return min(st[id][l][k],st[id][r-(1<<k)+1][k]); } void init(int k) { for(int j = 1; (1<<j) <= n; j++){ for(int i = 1; i + (1<<j) - 1 <= n; i++){ st[k][i][j] = min(st[k][i][j - 1], st[k][i + (1<<(j-1))][j - 1]); } } } int main() { lg[0]=-1; for(int i=1;i<=305;++i) lg[i]=lg[i>>1]+1; int f; while(~scanf("%d%d%d",&n,&m,&f)) { memset(zhi,0,sizeof(zhi)); for(int i=1;i<=m;i++) memset(st[i],0x3f,sizeof(st[i])); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { int a; scanf("%d",&a); zhi[i][j]=zhi[i-1][j]+a; st[j][i][0]=a; } for(int i=1;i<=m;i++) init(i); int sum=0; int sum1=0; int maxi=-0x3f3f3f3f; int maxm=-0x3f3f3f3f; int x; for(int i=0;i<n;i++) { for(int j=i+1;j<=n;j++) { sum=0; sum1=0; int cnt=0; for(int r=1;r<=m;r++) { int k=zhi[j][r]-zhi[i][r]; int pre=sum; x=rmq(r,i+1,j); if(sum>0) { sum+=k; cnt+=j-i; } else { sum=k; cnt=j-i; } if(sum1>0) sum1+=k; else { sum1=k-x+f; } sum1=max(sum1,pre+k-x+f); if(sum1>maxm) { maxm=sum1; } if(cnt<n*m) { if(sum>maxm) maxm=sum; } } } } printf("%d\n",maxm ); } }
描述
Once upon a time, there was a little dog YK. One day, he went to an antique shop and was impressed by a beautiful picture. YK loved it very much.
However, YK did not have money to buy it. He begged the shopkeeper whether he could have it without spending money.
Fortunately, the shopkeeper enjoyed puzzle game. So he drew a n × m matrix on the paper with integer value ai,j in each cell. He wanted to find 4 numbers x, y, x2, and y2(x ≤ x2, y ≤ y2), so that the sum of values in the sub-matrix from (x, y) to (x2, y2) would be the largest.
To make it more interesting, the shopkeeper ordered YK to change exactly one cell's value into P, then to solve the puzzle game. (That means, YK must change one cell's value into P.)
If YK could come up with the correct answer, the shopkeeper would give the picture to YK as a prize.
YK needed your help to find the maximum sum among all possible choices.
输入
There are multiple test cases.
The first line of each case contains three integers n, m and P. (1 ≤ n, m ≤ 300, -1000 ≤ P ≤ 1000).
Then next n lines, each line contains m integers, which means ai,j (-1000 ≤ ai,j ≤ 1000).
输出
For each test, you should output the maximum sum.
本文介绍了一种解决最大子矩阵和问题的方法,该问题要求在给定矩阵中找到最大和的子矩阵,允许更改矩阵中的一个元素值。通过使用区间最小值查询(RMQ)优化算法,文章详细阐述了解决方案的实现细节。
&spm=1001.2101.3001.5002&articleId=78092513&d=1&t=3&u=1bc3148304464503b2208c4968f3d72b)
950

被折叠的 条评论
为什么被折叠?



