-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_subarray.cpp
151 lines (125 loc) · 3.2 KB
/
max_subarray.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <cassert>
#include "max_subarray.hpp"
#include "my_tools.hpp"
using namespace std;
static void print_res(const char *s, const vector<int> &a, const MaxSubarrayResult &res)
{
cout << s << ": ";
for (int i = res.left; i <= res.right; i++) {
cout << a[i] << " ";
}
cout << ", sum = " << res.sum << endl;
}
static MaxSubarrayResult max_subarray_brute(const vector<int> &a)
{
ScopedClock cl(__FUNCTION__);
MaxSubarrayResult res(0, 0, a[0]);
for (int i = 0; i < (int)a.size(); i++) {
long long sum = 0;
for (int j = i; j < (int)a.size(); j++) {
sum += a[j];
if (sum > res.sum) {
res.sum = sum;
res.left = i;
res.right = j;
}
}
}
return res;
}
static MaxSubarrayResult max_subarray_sides(const vector<int> &a, int left, int mid, int right)
{
int best_left = mid;
int best_right = mid + 1;
long long max_sum_left = a[mid];
long long max_sum_right = a[mid + 1];
long long sum;
sum = 0;
for (int i = mid; i >= left; i--) {
sum += a[i];
if (sum > max_sum_left) {
max_sum_left = sum;
best_left = i;
}
}
sum = 0;
for (int i = mid + 1; i <= right; i++) {
sum += a[i];
if (sum > max_sum_right) {
max_sum_right = sum;
best_right = i;
}
}
return MaxSubarrayResult(best_left, best_right, max_sum_left + max_sum_right);
}
static MaxSubarrayResult max_subarray_divide_recursive(const vector<int> &a, int left, int right)
{
if (left >= right) return MaxSubarrayResult(left, right, a[left]);
int mid = (left + right)/2;
MaxSubarrayResult res1 = max_subarray_divide_recursive(a, left, mid);
MaxSubarrayResult res2 = max_subarray_divide_recursive(a, mid + 1, right);
MaxSubarrayResult res3 = max_subarray_sides(a, left, mid, right);
if (res1.sum >= res2.sum && res1.sum >= res3.sum) {
return res1;
} else if (res2.sum >= res1.sum && res2.sum >= res3.sum) {
return res2;
} else {
return res3;
}
}
static MaxSubarrayResult max_subarray_divide(const vector<int> &a)
{
ScopedClock cl(__FUNCTION__);
return max_subarray_divide_recursive(a, 0, (int)a.size()-1);
}
MaxSubarrayResult max_subarray_dynamic(const vector<int> &a)
/* Kadane algorithm */
{
ScopedClock cl(__FUNCTION__);
long long max_so_far = a[0];
long long max_sum = a[0];
int candidate_left = 0;
int left = 0;
int right = 0;
for (int i = 1; i < (int)a.size(); i++) {
if (max_so_far < 0) { // a[i] > a[i] + max_so_far
max_so_far = a[i];
candidate_left = i;
} else {
max_so_far = a[i] + max_so_far;
}
if (max_so_far > max_sum) {
max_sum = max_so_far;
right = i;
left = candidate_left;
}
}
return MaxSubarrayResult(left, right, max_sum);
}
void test_max_subarray()
{
for (int i = 0; i < 100; i++) {
int n = rand() % 10000 + 1;
vector<int> a(n);
for (int j = 0; j < n; j++) {
a[j] = rand() % 200 - 100;
}
MaxSubarrayResult res1 = max_subarray_brute(a);
MaxSubarrayResult res2 = max_subarray_divide(a);
MaxSubarrayResult res3 = max_subarray_dynamic(a);
if (res1.sum != res2.sum) {
print_res("brute", a, res1);
print_res("divide", a, res2);
assert(0);
}
if (res1.sum != res3.sum) {
print_res("brute", a, res1);
print_res("dynamic", a, res3);
assert(0);
}
}
}