-
Notifications
You must be signed in to change notification settings - Fork 0
/
#014:Longest_Collatz_sequence.cpp
72 lines (61 loc) · 1.62 KB
/
#014:Longest_Collatz_sequence.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
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
long getSquenceSize(long n)
{
int x = 0;
for (long i = 0; n > 1; i++)
{
if (n % 2)
n = 3 * n + 1;
else
n /= 2;
x++;
}
return (x);
}
int main() {
int n, x;
vector<int> v, res;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> x;
v.push_back(x);
}
x = *max_element(v.begin(), v.end());
// get Sequence size for every number smaller or equal x
for (int i = 1; i <= x; i++)
res.push_back(getSquenceSize(i));
vector<int> v_sort = v, res2;
sort(v_sort.begin(), v_sort.end());
// find max
auto it = max_element(res.begin(), res.begin() + v_sort[0]);
it = find_end(res.begin(), res.begin() + v_sort[0], it, it + 1);
res2.push_back(distance(res.begin(), it + 1));
auto tmp = it;
for (int i = 1; i < n; i++)
{
it = max_element(res.begin() + v_sort[i - 1], res.begin() + v_sort[i]);
if (*tmp > *it)
it = tmp;
else
{
it = find_end(res.begin() + v_sort[i - 1], res.begin() + v_sort[i], it, it + 1);
tmp = it;
}
res2.push_back(distance(res.begin(), it + 1));
while (i < (n - 1) && v[i] == v[i + 1]) {
i++;
res2.push_back(distance(res.begin(), it + 1));
}
}
// print result
for (int i = 0; i < n; i++)
{
it = find(v_sort.begin(), v_sort.end(), v[i]);
cout << res2[distance(v_sort.begin(), it)] << endl;
}
return 0;
}