Skip to content

Commit f491547

Browse files
committed
Update LIS
1 parent d705f0f commit f491547

File tree

2 files changed

+36
-70
lines changed

2 files changed

+36
-70
lines changed

BinSearch.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ bool check(int x) {
1010
int search_first(int l, int r) {
1111
int mid;
1212
while (l < r) {
13-
mid = (l + r) >> 1;
13+
mid = (l + r) >> 1; // (l + (r - l)) >> 1
1414
if (check(mid))
1515
r = mid;
1616
else
@@ -23,7 +23,7 @@ int search_first(int l, int r) {
2323
int search_last(int l, int r) {
2424
int mid;
2525
while (l < r) {
26-
mid = (l + r + 1) >> 1;
26+
mid = (l + r + 1) >> 1; // (l + (r - l) + 1) >> 1
2727
if (check(mid))
2828
l = mid;
2929
else

LIS.cpp

+34-68
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,41 @@
22

33
using namespace std;
44

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;
4818
}
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;
5619

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;
7038
}
71-
B[x] = {A[i], i};
39+
40+
return lis.size();
7241
}
73-
cout << ans << "\n";
74-
printStack(idx);
75-
return 0;
76-
}
42+
};

0 commit comments

Comments
 (0)