-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathFlags.cpp
More file actions
46 lines (41 loc) · 1.05 KB
/
Flags.cpp
File metadata and controls
46 lines (41 loc) · 1.05 KB
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
vector<bool> getPeaks(vector<int>& A)
{
int n = A.size();
vector<bool> P(A.size(), false);
for (int x = 1; x < n - 1; x++)
if (A[x] > A[x - 1] && A[x] > A[x + 1])
P[x] = true;
return P;
}
vector<int> getNext(vector<int> &A, vector<bool> &peaks) {
vector<int> next(A.size(), 0);
next[A.size()-1] = -1;
for (int i = A.size() - 2; i >= 0; i--) {
if (peaks[i])
next[i] = i;
else
next[i] = next[i + 1];
}
return next;
}
int solution(vector<int>& A) {
if (A.size() < 3) return 0;
const int N = A.size();
vector<bool> P = getPeaks(A);
vector<int> next = getNext(A, P);
int max_flags = 0;
for (int d = 1; d * (d-1) <= N; d++)
{
int current_flags = 0;
int pos = 0;
while (pos < N && current_flags < d) {
pos = next[pos];
if (pos == -1) //end
break;
pos += d;
current_flags++;
}
max_flags = max(max_flags, current_flags);
}
return max_flags;
}