-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·82 lines (64 loc) · 1.83 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·82 lines (64 loc) · 1.83 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
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
#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
void read_data(std::vector<int> &data, size_t &element_number)
{
data.reserve(element_number);
for (size_t i = 0; i < element_number; ++i)
{
int input_value;
std::cin >> input_value;
data.push_back(input_value);
}
}
void count_LIS(std::vector<int> &index, const std::vector<int> &data,
size_t element_number)
{
std::vector<int> ending(element_number + 1, INT_MAX);
std::vector<int> min_position(element_number + 1, INT_MAX);
std::vector<int> previous(element_number, INT_MAX);
index.clear();
ending[0] = INT_MIN;
previous[0] = INT_MAX;
size_t max_index = 0;
for (size_t i = 0; i < element_number; ++i)
{
size_t current_pos = (std::lower_bound(ending.begin(), ending.end(), data[i]) - ending.begin());
if (ending[current_pos - 1] < data[i] && ending[current_pos] > data[i])
{
ending[current_pos] = data[i];
min_position[current_pos] = i;
if (ending[current_pos] > ending[max_index])
max_index = current_pos;
previous[i] = min_position[current_pos - 1];
}
}
size_t i = min_position[max_index];
while (i != INT_MAX)
{
index.push_back(data[i]);
i = previous[i];
}
std::reverse(index.begin(), index.end());
}
void write_answer(const std::vector<int> &answer)
{
for (auto result:answer)
{
std::cout << result << ' ';
}
std::cout << '\n';
}
int main()
{
std::ios_base::sync_with_stdio(false);
size_t element_number;
std::cin >> element_number;
std::vector<int> data;
read_data(data, element_number);
std::vector<int> answer;
count_LIS(answer, data, element_number);
write_answer(answer);
return 0;
}