BlackWuKong @ 2024-08-10 17:19:33
#include<bits/stdc++.h>
using namespace std;
int n,a[125][125],b[125][125],ans=-1e9;
int main(){
cin>>n;
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
cin>>a[i][j];
if (j==1) b[i][j]=b[i-1][n]+a[i][j];
else b[i][j]=b[i][j-1]+a[i][j];
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
if (j==1) ans=max(b[i][j]-b[i-1][n],ans);
else ans=max(b[i][j]-b[i][j-1],ans);
}
}
cout<<ans;
return 0;
}
by BlackWuKong @ 2024-08-10 17:30:47
更新了一个版本(虽然依旧没过样例)
#include<bits/stdc++.h>
using namespace std;
int n,a[125][125],b[125][125],ans=-1e9;
int main(){
cin>>n;
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
cin>>a[i][j];
if (j==1) b[i][j]=b[i-1][n]+a[i][j];
else b[i][j]=b[i][j-1]+a[i][j];
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
for (int x=1;x<=n;x++){
for (int y=1;y<=n;y++){
if (j==1) ans=max(b[x][y]-b[i-1][n],ans);
else ans=max(b[x][y]-b[i][j-1],ans);
}
}
}
}
cout<<ans;
return 0;
}
by LiZeChen_1 @ 2024-08-10 17:33:18
这个应该可以过
#include<bits/stdc++.h>
using namespace std;
int n, a[125][125], b[125][125], ans = INT_MIN;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
b[i][j] = a[i][j];
if (i > 0) b[i][j] += b[i - 1][j];
if (j > 0) b[i][j] += b[i][j - 1];
if (i > 0 && j > 0) b[i][j] -= b[i - 1][j - 1];
}
}
for (int i1 = 0; i1 < n; i1++) {
for (int j1 = 0; j1 < n; j1++) {
for (int i2 = i1; i2 < n; i2++) {
for (int j2 = j1; j2 < n; j2++) {
int sum = b[i2][j2];
if (i1 > 0) sum -= b[i1 - 1][j2];
if (j1 > 0) sum -= b[i2][j1 - 1];
if (i1 > 0 && j1 > 0) sum += b[i1 - 1][j1 - 1];
ans = max(ans, sum);
}
}
}
}
cout << ans;
return 0;
}
by LiZeChen_1 @ 2024-08-10 17:33:50
@LiZeChen_1 感觉这个时间复杂度有点恐怖
by LiZeChen_1 @ 2024-08-10 17:34:41
@lanlingxuan 我试了,AC了
by BlackWuKong @ 2024-08-10 17:41:15
THANK YOU
已关注