标签:遍历 利用 严格 字段 else poj stream tmp pre
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n;
cin>>n;
int maxans=-0x3f3f3f3f,tmpans=0;
while(n--){
int tmp;
cin>>tmp;
if(tmpans<0) tmpans=tmp;
else tmpans+=tmp;
maxans=max(maxans,tmpans);
}
cout<<maxans;
return 0;
}
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main(){
int n;
cin>>n;
int a[505][505];
memset(a,0,sizeof(a));
//让坐标有0的部分为0,不影响数据,简化处理
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
a[i][j]+=a[i][j-1];//前缀和预处理
}
}
int sum=0,ans=-0x3f3f3f3f;//初始化
for(int i=1;i<=n;i++){
for(int j=i;j<=n;j++){
sum=0;
for(int k=1;k<=n;k++){
if(sum<0) sum=a[k][j]-a[k][i-1];
//利用最大子段和思想处理最大矩阵和
else sum+=a[k][j]-a[k][i-1];
ans=max(ans,sum);
}
}
}
cout<<ans;
return 0;
}
【算法竞赛进阶指南】扩展最大子段和POJ1050ToTheMax
标签:遍历 利用 严格 字段 else poj stream tmp pre
原文地址:https://www.cnblogs.com/rign/p/10059750.html