前置:
二分查詢, 最長單調不升子序列,最長單調不降子序列(dilworth)。
題解:
可以用來練習手寫二分,二分最佳化的最長上升子序列時間複雜度O(nlogn)。但是坑是非常多的。
程式碼:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int a[N], n, b[N], bb[N];
int l1 = 1, l2 = 1;
int find(int x){
int l = 0, r = l1 + 1;
while(l + 1 != r){
int mid = (l + r) >> 1;
if(b[mid] < x)r = mid;
else l = mid;
}
return r;
}
int find2(int x){
int l = 0, r = l2 + 1;
while(l + 1 != r){
int mid = (l + r) >> 1;
if(bb[mid] >= x)r = mid;
else l = mid;
}
return r;
}
void solve() {
while(cin >> a[++n]);
b[1] = a[1];
bb[1] = a[1];
for(int i = 2; i < n; i++){
if(a[i] <= b[l1])b[++l1] = a[i];
else b[find(a[i])] = a[i];
if(a[i] > bb[l2])bb[++l2] = a[i];
else bb[find2(a[i])] = a[i];
}
cout << l1 << '\n' << l2;
}
int main() {
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int _ = 1; //cin >> _;
while (_--) solve();
}