Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions dynamic-programming/lis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
// dp(i) = max j<i { dp(j) | a[j] < a[i] } + 1
//

// int dp[N], v[N], n, lis;
int lis(vector<int>& a) {
vector<int> dp (a.size(), 0x3f3f3f3f);
int res = 0;

memset(dp, 63, sizeof dp);
for (int i = 0; i < n; ++i) {
// increasing: lower_bound
// non-decreasing: upper_bound
int j = lower_bound(dp, dp + lis, v[i]) - dp;
dp[j] = min(dp[j], v[i]);
lis = max(lis, j + 1);
for (int v : a) {
// increasing: lower_bound
// non-decreasing: upper_bound
int j = lower_bound(dp.begin(), dp.begin() + res, v) - dp.begin();
dp[j] = min(dp[j], v);
res = max(res, j + 1);
}

return res;
}
9 changes: 5 additions & 4 deletions tests/dynamic-programming/lis.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
#include<bits/stdc++.h>
using namespace std;

// @include: dynamic-programming/lis.cpp

const int N = 2e5;

int main() {
int dp[N], v[N], n, lis;
int n;

cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++)
cin >> v[i];

// @include: dynamic-programming/lis.cpp

cout << lis << endl;
cout << lis(v) << endl;
}