Created
March 19, 2021 12:06
-
-
Save vivan188/45895b6f09fc4f6b692a73f459d760c3 to your computer and use it in GitHub Desktop.
Solution to LIS using DP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
const int maxn = 1e5+10; | |
int a[maxn], lis[maxn], lds[maxn]; | |
int l[maxn], r[maxn], lcnt[maxn]; | |
int main(){ | |
int n; | |
cin >> n; | |
memset(lis, 63, sizeof(lis)); | |
memset(lds, 63, sizeof(lds)); | |
for(int i = 0; i < n; i++){ | |
cin >> a[i]; | |
l[i] = lower_bound(lis, lis+n, a[i]) - lis; | |
lis[l[i]] = a[i]; | |
} | |
for(int i = n-1; i > -1; i--){ | |
r[i] = lower_bound(lds, lds+n, -a[i]) - lds; | |
lds[r[i]] = -a[i]; | |
} | |
int lissize = lower_bound(lis, lis+n, lis[maxn-1]) - lis; | |
for(int i = 0; i < n; i++) | |
if(l[i] + r[i] +1 >= lissize) | |
lcnt[l[i]]++; | |
for(int i = 0; i < n; i++){ | |
if(l[i] + r[i] +1 < lissize) | |
cout << 1; | |
else if(lcnt[l[i]] > 1) | |
cout << 2; | |
else | |
cout << 3; | |
} | |
cout << '\n'; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment