Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 188e717

Browse files
1, 2 and 3 D Kadane Algorithm
1 parent 0541c44 commit 188e717

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

‎Kadane Algorithm/1D-Kadane.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
const int N = 1e6 + 5, OO = 0x3f3f3f3f, MOD = 1000000007;
5+
const double EPS = 0.000000001;
6+
7+
int n;
8+
int A[N];
9+
long long sum, bestSum;
10+
11+
int main(){
12+
//freopen("i.in", "rt", stdin);
13+
//freopen("o.out", "wt", stdout);
14+
scanf("%d", &n);
15+
for(int i = 0 ; i < n ; ++i)
16+
scanf("%d", A+i);
17+
for(int i = 0 ; i < n ; ++i){
18+
sum += A[i];
19+
sum = max(sum, 0ll);
20+
bestSum = max(bestSum, sum);
21+
}
22+
printf("%lld\n", bestSum);
23+
return 0;
24+
}

‎Kadane Algorithm/2D-Kadane.cpp‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
const int N = 1e2 + 5, OO = 0x3f3f3f3f, MOD = 1000000007;
5+
const double EPS = 0.000000001;
6+
7+
int n, m;
8+
int A[N][N];
9+
long long bestSum;
10+
11+
int main(){
12+
//freopen("i.in", "rt", stdin);
13+
//freopen("o.out", "wt", stdout);
14+
scanf("%d %d", &n, &m);
15+
for(int i = 1 ; i <= n ; ++i){
16+
for(int j = 1 ; j <= m ; ++j){
17+
scanf("%d", A[i]+j);
18+
A[i][j] += A[i-1][j]; //Cummulative Columns.
19+
}
20+
}
21+
for(int top = 1 ; top <= n ; ++top){
22+
for(int btm = top ; btm <= n ; ++btm){
23+
long long sum = 0;
24+
for(int i = 1 ; i <= m ; ++i){
25+
sum += A[btm][i] - A[top-1][i]; //Add current element -Column-
26+
sum = max(sum, 0ll); //Does it worth?
27+
bestSum = max(bestSum, sum); //Is it the best?
28+
}
29+
}
30+
}
31+
printf("%lld\n", bestSum);
32+
return 0;
33+
}

‎Kadane Algorithm/3D-Kadane.cpp‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
const int N = 22 , OO = 0x3f3f3f3f, MOD = 1000000007;
5+
const double EPS = 0.000000001;
6+
7+
int a, b, c;
8+
long long A[N][N][N];
9+
long long bestSum;
10+
11+
int main(){
12+
//freopen("i.in", "rt", stdin);
13+
//freopen("o.out", "wt", stdout);
14+
scanf("%d %d %d", &a, &b, &c);
15+
for(int i = 1 ; i <= a ; ++i){
16+
for(int j = 1 ; j <= b ; ++j){
17+
for(int k = 1 ; k <= c ; ++k){
18+
scanf("%lld", A[i][j]+k);
19+
A[i][j][k] += A[i-1][j][k] + A[i][j-1][k] - A[i-1][j-1][k]; //Cummulative grids
20+
}
21+
}
22+
}
23+
for(int sa = 1 ; sa <= a ; ++sa)for(int ea = sa ; ea <= a ; ++ea){
24+
for(int sb = 1 ; sb <= b ; ++sb)for(int eb = sb ; eb <= b ; ++eb){
25+
long long sum = 0;
26+
for(int i = 1 ; i <= c ; ++i){
27+
sum += A[ea][eb][i] - A[sa-1][eb][i] - A[ea][sb-1][i] + A[sa-1][sb-1][i];
28+
sum = max(sum, 0ll); //Does it worth?
29+
bestSum = max(bestSum, sum); //Is it the best?
30+
}
31+
}
32+
}
33+
printf("%lld\n", bestSum);
34+
return 0;
35+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /