-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_counters.cpp
49 lines (41 loc) · 1 KB
/
max_counters.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
#include <vector>
#include <iostream>
using namespace std;
vector<int> solution(int N, vector<int> &A)
{
vector<int> counters(N, 0);
int upgrade_all = N + 1;
int max_counter = 0;
int max_tmp = -1;
for (std::size_t i = 0; i < A.size(); ++i) {
const auto k = A[i];
if (k >= 1 && k <= N) {
int &target_counter = counters[k - 1];
if (target_counter < max_tmp) {
target_counter = max_tmp;
}
++target_counter;
if (target_counter > max_counter) {
max_counter = target_counter;
}
} else if (A[i] == upgrade_all) {
max_tmp = max_counter;
}
}
for (auto &e: counters) {
if (e < max_tmp) {
e = max_tmp;
}
}
return counters;
}
int main()
{
std::vector<int> input = {3, 4, 4, 6, 1, 4, 4};
cout << "(";
for (auto e: solution(5, input)) {
cout << e << ", ";
}
cout << ")" << endl;
return 0;
}