-
Notifications
You must be signed in to change notification settings - Fork 0
/
Uva - 10827.cpp
84 lines (81 loc) · 1.36 KB
/
Uva - 10827.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <cstring>
using namespace std;
int mat[1000][1000];
int buff[1000];
int x,y;
int left,right,up, down;
int kadane(int *arr)
{
int UP = 0;
int DOWN = 0;
long long int maxSum = 0;
long long int maxArea = 0;
long long int sum = 0;
while(UP < x && DOWN < x)
{
if(sum + arr[DOWN] >=0 && DOWN - UP + 1 <= x/2)
{
sum+=arr[DOWN];
DOWN++;
if(sum > maxSum)
{
maxSum = sum;
}
}
else if(sum!=0)
{
if(sum > maxSum && sum != 0)
{
maxSum = sum;
}
UP++;
DOWN=UP;
sum = 0;
}
else
{
UP++;
DOWN=UP;
}
}
return maxSum;
}
int main() {
int t;
cin >> t;
while(t--)
{
cin >> x;
y =x;
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
cin >> mat[i][j];
mat[i+x][j] = mat[i][j];
mat[i][j+y] = mat[i][j];
mat[i+x][j+y] = mat[i][j];
}
}
x*=2;
y*=2;
int ans = 0, sol = 0;
for(int left=0;left<y;left++)
{
for(int i=0;i<x;i++) buff[i] = 0;
for(int right=left;right<y;right++)
{
for(int i=0;i<x;i++)
{
buff[i]+=mat[i][right];
}
if(right - left +1 <= x/2)
sol = kadane(buff);
if(sol > ans) ans = sol;
}
}
cout << ans << endl;
}
return 0;
}