|
2 | 2 |
|
3 | 3 | using namespace std;
|
4 | 4 |
|
5 |
| -const int N = 1e6, NT = N + 2; |
6 |
| - |
7 |
| -struct StackStruct { |
8 |
| - shared_ptr<StackStruct> prev; |
9 |
| - int head, size; |
10 |
| - StackStruct() { prev = nullptr; } |
11 |
| - StackStruct(shared_ptr<StackStruct> s, int e) { |
12 |
| - prev = s; |
13 |
| - head = e; |
14 |
| - } |
15 |
| -}; |
16 |
| - |
17 |
| -shared_ptr<StackStruct> historia[NT]; |
18 |
| -int t = 1; |
19 |
| - |
20 |
| -void push(int k, int e) { |
21 |
| - historia[t++] = make_shared<StackStruct>(historia[k], e); |
22 |
| -} |
23 |
| - |
24 |
| -void printStack(int k) { |
25 |
| - auto s = historia[k]; |
26 |
| - vector<int> stos; |
27 |
| - while (s) { |
28 |
| - stos.push_back(s->head); |
29 |
| - s = s->prev; |
30 |
| - } |
31 |
| - reverse(stos.begin(), stos.end()); |
32 |
| - for (const int &e : stos) { |
33 |
| - cout << e << " "; |
34 |
| - } |
35 |
| - cout << "\n"; |
36 |
| -} |
37 |
| - |
38 |
| -int A[NT]; |
39 |
| -pair<int, int> B[NT]; |
40 |
| - |
41 |
| -int find(int l, int r, int val) { |
42 |
| - while (l < r) { |
43 |
| - int mid = (l + (r - l)) >> 1; |
44 |
| - if (val < B[mid].first) |
45 |
| - r = mid; |
46 |
| - else |
47 |
| - l = mid + 1; |
| 5 | +class LIS { |
| 6 | +public: |
| 7 | + int findNewPosition(const vector<int> &lis, int num) { |
| 8 | + int l = 0, r = lis.size() - 1, mid; |
| 9 | + |
| 10 | + while (l < r) { |
| 11 | + mid = (l + r) >> 1; |
| 12 | + if (num < lis[mid]) |
| 13 | + r = mid; |
| 14 | + else |
| 15 | + l = mid + 1; |
| 16 | + } |
| 17 | + return l; |
48 | 18 | }
|
49 |
| - return l; |
50 |
| -} |
51 |
| - |
52 |
| -signed main() { |
53 |
| - ios_base::sync_with_stdio(0); |
54 |
| - cin.tie(0); |
55 |
| - int n, ans = 1, idx = 0; |
56 | 19 |
|
57 |
| - cin >> n; |
58 |
| - for (int i = 1; i <= n; i++) { |
59 |
| - cin >> A[i]; |
60 |
| - } |
61 |
| - for (int i = 1; i <= n; i++) { |
62 |
| - B[i].first = LONG_LONG_MAX; |
63 |
| - } |
64 |
| - for (int i = 1; i <= n; i++) { |
65 |
| - int x = find(1, n, A[i]); // szukamy 1szego miejsca ze A[i] < B[x].first |
66 |
| - push(B[x - 1].second, A[i]); // dodajemy element do poprzedniego stosu |
67 |
| - if (x >= ans) { |
68 |
| - ans = x; |
69 |
| - idx = i; |
| 20 | + int lengthOfLIS(const vector<int> &nums) { |
| 21 | + int n = nums.size(); |
| 22 | + if (n <= 1) |
| 23 | + return n; |
| 24 | + |
| 25 | + vector<int> lis; |
| 26 | + lis.push_back(nums[0]); |
| 27 | + for (const int &num : nums) { |
| 28 | + if (num > lis[lis.size() - 1]) { |
| 29 | + lis.push_back(num); |
| 30 | + continue; |
| 31 | + } |
| 32 | + if (num == lis[lis.size() - 1]) |
| 33 | + continue; |
| 34 | + int pos = findNewPosition(lis, num); |
| 35 | + if (pos > 0 && lis[pos - 1] == num) |
| 36 | + continue; |
| 37 | + lis[pos] = num; |
70 | 38 | }
|
71 |
| - B[x] = {A[i], i}; |
| 39 | + |
| 40 | + return lis.size(); |
72 | 41 | }
|
73 |
| - cout << ans << "\n"; |
74 |
| - printStack(idx); |
75 |
| - return 0; |
76 |
| -} |
| 42 | +}; |
0 commit comments