此题和 P1719 最大加权矩形 解法几乎相同,而 P1719 为橙题
此题代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int juzhen[1010][1010];
int qzh[1010][1010];
int n,m,c,maxx,temp,ansi,ansj;
signed main(){
cin>>n>>m>>c;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>juzhen[i][j];
qzh[i][j]=qzh[i-1][j]+qzh[i][j-1]-qzh[i-1][j-1]+juzhen[i][j];
}
}
for(int i=1;i<=n-c+1;i++){
for(int j=1;j<=m-c+1;j++){
temp=qzh[i+c-1][j+c-1]-qzh[i-1][j+c-1]-qzh[i+c-1][j-1]+qzh[i-1][j-1];
if(temp>=maxx){
maxx=temp;
ansi=i;
ansj=j;
}
}
}
cout<<ansi<<' '<<ansj;
return 0;
}
P1719代码:
#include <bits/stdc++.h>
using namespace std;
int juzhen[130][130];
int qzh[130][130];
int qzh2[130][130];
int n,maxx,temp;
int main(){
cin>>n;
memset(qzh,0,sizeof(qzh));
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>juzhen[i][j];
qzh[i][j]=qzh[i][j-1]+juzhen[i][j];
for(int k=1;k<=i;k++){
qzh2[i][j]+=qzh[k][j];
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
for(int ii=i;ii<=n;ii++){
for(int jj=j;jj<=n;jj++){
//2 1 4 2
//2 2 3 3
/*
/ 0 1 2 3 4
0 0 0 0 0 0
1 0 0 -2 -7 0
2 0 9 2 -6 2
3 0 -4 1 -4 1
4 0 -1 8 0 -2
*/
temp=qzh2[ii][jj]-qzh2[i-1][jj]-qzh2[ii][j-1]+qzh2[i-1][j-1];
maxx=max(temp,maxx);
}
}
}
}
cout<<maxx;
return 0;
}