最长递增子序列
int BiSearch(int len, int w) { int left = 0, right = len - 1; int mid; while (left <= right) { mid = left + (right-left)/2; if (bz[mid] > w) right = mid - 1; else if (bz[mid] < w) left = mid + 1; else return mid; } return left; } int LIS(int n) //n为arr数组的长度 从0开始存 { len = 1; bz[0] = arr[0]; int pos = 0; for(int i=1; i<n; ++i) { if(arr[i] > bz[len-1]) { bz[len] = arr[i]; ++len; } else { pos = BiSearch(len, arr[i]); bz[pos] = arr[i]; } } return len; }
int d[]; int res[]; int now=1; d[1]=res[0]; int len; for(int i=1;i<len;i++) { if(res[i]>d[now]) { d[++now]=res[i]; } else { int j=lower_bound(d+1,d+now,res[i])-d; d[j]=res[i]; } }
>为严格递增
/*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_back using namespace std; typedef long long ll; typedef unsigned long long ull; const ll LLmaxn = 2e18; const int N = 1e5 + 5; inline void read(int &v) { v = 0; char c = 0; int p = 1; while (c < '0' || c > '9') { if (c == '-') { p = -1; } c = getchar(); } while (c >= '0' && c <= '9') { v = (v << 3) + (v << 1) + c - '0'; c = getchar(); } v *= p; } int n; int num[N]; int lis[N]; int nowmaxn[N]; int aim; int pop=1; int main() { int n; read(n); for(int i=1;i<=n;i++) { read(num[i]); } nowmaxn[1]=num[1]; lis[1]=1; for(int i=2;i<=n;i++) { if(num[i]>nowmaxn[pop]) { nowmaxn[++pop]=num[i]; lis[i]=pop; } else { aim=lower_bound(nowmaxn,nowmaxn+pop,num[i])-nowmaxn; lis[i]=aim; nowmaxn[aim]=num[i]; } } for(int i=1;i<=n;i++) { cout<<lis[i]<<" "; } cout<<endl; cout<<pop<<endl; return 0; }