前置:
二分查找, 最长单调不升子序列,最长单调不降子序列(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();
}
标签:bb,int,mid,else,while,P1020,NOIP1999,l2,DP From: https://www.cnblogs.com/Amire/p/18370590