Skip to content

Commit

Permalink
Merge branch 'main' of github.com:iagorrr/Competitive-Programming-Alg…
Browse files Browse the repository at this point in the history
…orithms
  • Loading branch information
iagorrr committed May 19, 2024
2 parents 8bb2a68 + a9d5447 commit 200c192
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions algorithms/dynamic-programming/longest-increasing-subsequence.cpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
template<typename T>
template <typename T>
pair<int, vector<int>> lis(const vector<T> &xs) {
int n = xs.size();
int n = xs.size();

vector<T> dp(n+1, numeric_limits<T>::max());
dp[0] = numeric_limits<T>::min();
vector<T> dp(n + 1, numeric_limits<T>::max());
dp[0] = numeric_limits<T>::min();

int sz = 0;
vector<int> psx(n);
int sz = 0;
vector<int> psx(n);

for (int i = 0; i < n; i++) {
auto it = lower_bound(dp.begin(), dp.end(), xs[i]);
auto pos = (int)(it - dp.begin());
for (int i = 0; i < n; i++) {
auto it = lower_bound(dp.begin(), dp.end(), xs[i]);
auto pos = (int)(it - dp.begin());

sz = max(sz, pos);
sz = max(sz, pos);

dp[pos] = xs[i];
dp[pos] = xs[i];

psx[i] = pos;
}
psx[i] = pos;
}

return {sz, psx};
return {sz, psx};
}

template<typename T>
template <typename T>
vector<int> get_idx(vector<T> xs) {
auto [sz1, psx1] = lis(xs);
auto [sz1, psx1] = lis(xs);

reverse(xs.begin(), xs.end());
for (auto &xi : xs) xi = -xi;
reverse(xs.begin(), xs.end());
for (auto &xi : xs) xi = -xi;

auto [sz2, psx2] = lis(xs);
auto [sz2, psx2] = lis(xs);

vector<int> ans;
int _n = xs.size();
for (int i = 0; i < _n; i++) {
int l = psx1[i];
int r = psx2[_n-i-1];
if (l + r -1 == sz1) ans.push_back(i);
}

return ans;
vector<int> ans;
int _n = xs.size();
for (int i = 0; i < _n; i++) {
int l = psx1[i];
int r = psx2[_n - i - 1];
if (l + r - 1 == sz1) ans.push_back(i);
}

return ans;
}
Binary file modified notebook.pdf
Binary file not shown.

0 comments on commit 200c192

Please sign in to comment.