-
Notifications
You must be signed in to change notification settings - Fork 0
/
Uva - 119551.cpp
94 lines (92 loc) · 1.74 KB
/
Uva - 119551.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
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <cstring>
#include <climits>
using namespace std;
long long int mat[1000][1000];
int x, resArea = 0, resSum = 0;
void kadane(long long int *arr,int l, int r)
{
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] <=k)
{
sum+=arr[DOWN];
DOWN++;
int area = (DOWN- UP) * ( r - l + 1);
if(area > maxArea)
{
maxSum = sum;
maxArea = area;
}
if(area == maxArea && sum < maxSum) maxSum = sum;
}
else if(sum!=0)
{
int area = (DOWN-1 - UP + 1) * ( r - l + 1);
if(area > maxArea && sum != 0)
{
maxSum = sum;
maxArea = area;
}
if(area == maxArea && sum < maxSum) maxSum = sum;
UP++;
DOWN=UP;
sum = 0;
}
else
{
UP++;
DOWN=UP;
}
}
if(resArea < maxArea)
{
resArea = maxArea;
resSum = maxSum;
}
if(resArea == maxArea && resSum > maxSum)
resSum = maxSum;
}
int main() {
int t;
cin >> t;
int CASE = 1;
while(t--)
{
long long int buff[1000];
int y,k;
resArea = 0;
resSum = 0;
cin >> x >> y >> k;
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
cin >> mat[i][j];
}
}
long long int sol = INT_MAX;
long long int area = 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];
}
up=0; down=0;
kadane(buff,left+1,right+1);
}
}
cout << "Case #"<<CASE <<": " << resArea << " " << resSum << endl;
CASE++;
}
return 0;
}