-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxSubMatrix(DAA).cpp
58 lines (53 loc) · 986 Bytes
/
MaxSubMatrix(DAA).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
int main()
{
//ifstream file("Test.txt");
int N;
cin>>N;
//file>>N;
vector<vector<int> > matrix(N, vector<int>(N));
for(int i = 0;i<N;++i)
{
for(int j = 0;j<N;++j)
{
cin>>matrix[i][j];
//file>>matrix[i][j];
}
}
int maxSum = -101;
for(int l = 0;l<N;++l)
{
vector<int> subArr(N);
for(int j = l;j<N;++j)
{
for(int k = 0;k<N;++k)
subArr[k]+=matrix[j][k];
int lastSum = 0, sum = -101, maxVal = -101;
for(int i = 0;i<N;++i)
{
maxVal = (maxVal<subArr[i])?subArr[i]:maxVal;
if(subArr[i]<0&&lastSum==0)
continue;
if(lastSum+subArr[i]>=0)
{
sum = (sum<lastSum)?lastSum:sum;
lastSum+=subArr[i];
}
else
{
sum = (sum<lastSum)?lastSum:sum;
lastSum = 0;
}
}
sum = (sum<lastSum)?lastSum:sum;
if(sum==0&&maxVal<0)
sum = maxVal;
maxSum = (maxSum<sum)?sum:maxSum;
}
}
cout<<maxSum<<endl;
return 0;
}