This repository was archived by the owner on Nov 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFindMaximumSubarray.cpp
97 lines (86 loc) · 2.76 KB
/
FindMaximumSubarray.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
//
// algorithm - some algorithms in "Introduction to Algorithms", third edition
// Copyright (C) 2018 lxylxy123456
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef MAIN
#define MAIN
#define MAIN_FindMaximumSubarray
#endif
#ifndef FUNC_FindMaximumSubarray
#define FUNC_FindMaximumSubarray
#include <algorithm>
#include "utils.h"
template <typename T>
class sinfo {
public:
sinfo(size_t a, size_t b, T c): begin(a), end(b), sum(c) {}
bool operator<(const sinfo& rhs) const { return sum < rhs.sum; }
size_t begin;
size_t end;
T sum;
};
template <typename T>
sinfo<T> FindMaxCrossingSubarray(std::vector<T>& A,size_t l,size_t m,size_t h) {
auto left = std::min_element(A.begin() + l, A.begin() + m);
auto right = std::max_element(A.begin() + m, A.begin() + h);
return sinfo<T>(left - A.begin(), right + 1 - A.begin(), *right - *left);
}
template <typename T>
sinfo<T> FindMaximumSubarray(std::vector<T>& A, size_t l, size_t h) {
if (l == h - 1) {
return sinfo<T>(l, h, A[l]);
} else {
size_t m = (l + h) / 2;
sinfo<T> si1 = FindMaximumSubarray(A, l, m);
sinfo<T> si2 = FindMaximumSubarray(A, m, h);
sinfo<T> si3 = FindMaxCrossingSubarray(A, l, m, h);
return std::max(std::max(si1, si2), si3);
}
}
template <typename T>
sinfo<T> FindMaximumSubarray(std::vector<T>& A) {
return FindMaximumSubarray(A, 0, A.size());
}
template <typename T, T T0>
sinfo<T> FindMaximumSubarray_On(std::vector<T>& A) {
sinfo<T> best(0, 0, T0);
size_t bci = 0; // best continuing index
T bcv = T0; // best continuing value
for (size_t i = 0; i < A.size(); i++) {
// sum([bci:i]) = bcv
if (bcv < 0) {
bci = i;
bcv = T0;
}
bcv += A[i];
best = std::max(best, sinfo<T>(bci, i + 1, bcv));
}
return best;
}
#endif
#ifdef MAIN_FindMaximumSubarray
int main(int argc, char *argv[]) {
std::vector<int> a;
int n = get_argv(argc, argv, 1, 10);
random_integers(a, -n, n, n);
output_integers(a);
// sinfo<int> ans = FindMaximumSubarray(a);
sinfo<int> ans = FindMaximumSubarray_On<int, 0>(a);
std::vector<int> sub_array(a.begin() + ans.begin, a.begin() + ans.end);
output_integers(sub_array);
return 0;
}
#endif