模板—LIS(最长上升子序列优化算法)
链接:HDU - 5532
https://vjudge.net/contest/161167#problem/F
复杂度 n*logn
二分查找+dp
输入n n个数
查找n个数的最长上升子序列
方法一:STL版
int LIS(void)
{
int tail[100005],len = 0;
//最长下降子序列 改为for(int i=n-1;i>=0;i--)
//或 reverse(a,a+n)
for(int i=0;i<n;i++)
{
int pos = upper_bound(tail,tail+len,a[i])-tail;
tail[pos] = a[i];
if(pos==len)len++;
}
return len;
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
return 0;
}
方法2:
一个类似的但是长度大于等于n-1
#include <iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<queue>
#include<deque>
#include<map>
using namespace std;
int n,mmax;
int link[100002],along[100002];
int a[100002],b[100002];
int find(int key)
{
int l = 1,r = mmax,mid;
while(l<=r)
{
mid = (l+r)/2;
if(a[along[mid]]<=key)
l = mid+1;
else r = mid-1;
}
return l -1;
}
int find2(int key)
{
int l = 1,r = mmax,mid;
while(l<=r)
{
mid = (l+r)/2;
if(b[along[mid]]<=key)
l = mid+1;
else r = mid-1;
}
return l -1;
}
int main()
{
int t,l;
cin>>t;
while(t--)
{
//int n;
cin>>n;
//vector<int>o;
int long1,long2;
int dp1[100002],dp2[100002];
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
b[n-i+1] = a[i];
// dp1[i] = dp2[i] =1;
}
mmax = 1;
along[mmax] = 1;
link[1] =1;
for(int i=2;i<=n;i++)
{
if(a[i]>=a[along[mmax]])
{
mmax++;
along[mmax] = i;
link[i] = along[mmax-1];
}
else
{
l = find(a[i]);
along[l+1] = i;
link[i]= along[l];
}
}
long1 = mmax;
mmax = 1;
along[mmax] = 1;
link[1] =1;
for(int i=2;i<=n;i++)
{
if(b[i]>=b[along[mmax]])
{
mmax++;
along[mmax] = i;
link[i] = along[mmax-1];
}
else
{
l = find2(b[i]);
along[l+1] = i;
link[i]= along[l];
}
}
long2 = mmax;
//cout<<long1<<" "<<long2<<endl;
if(long1>=n-1||long2>=n-1)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}