Tim_justforsure @ 2023-07-14 15:06:32
能过样例
#include<iostream>
using namespace std;
const int N=1e3+5;
int n,m;
int sum[N][N],a[N][N];
int main(){
cin>>n;
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
cin>>a[i][j];
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+a[i][j];
}
}
int ans=0;
for (int x1=1;x1<=n;x1++){
for (int y1=1;y1<=n;y1++){
for (int x2=x1;x2<=n;x2++){
for (int y2=y1;y2<=n;y2++){
ans=max(ans,sum[x2][y2]-sum[x1-1][y2]+sum[x1-1][y1-1]);
}
}
}
}
cout<<ans;
return 0;
}
by RicardoMZhou @ 2023-07-14 15:07:26
6
by FengYuXinMing @ 2023-07-14 15:11:11
@Tim0303 少一步
by RicardoMZhou @ 2023-07-14 15:11:33
#include<iostream>
#define INF 0x3f3f3f3f
using namespace std;
const int N=1e3+5;
int n,m;
int sum[N][N],a[N][N];
int main(){
cin>>n;
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
cin>>a[i][j];
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+a[i][j];
}
}
int ans=0;
for (int x1=1;x1<=n;x1++){
for (int y1=1;y1<=n;y1++){
for (int x2=x1;x2<=n;x2++){
for (int y2=y1;y2<=n;y2++){
ans=max(ans,sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1]);
}
}
}
}
cout<<ans;
return 0;
}
by RicardoMZhou @ 2023-07-14 15:12:18
调完了@ Tim0303
by Tim_justforsure @ 2023-07-14 15:13:17
@RicardoMZhou ok
by FengYuXinMing @ 2023-07-14 15:13:36
要在4层循环里面,第二个运算之后加一个-sum x2 y1-1