最长上升子序列

只有nlogn

Posted by Aspe on July 3, 2018
#include <iostream>
#include <cstdio>

using namespace std;

const int oo=233333333;
const int ML=100100;

int tp;
int f[ML];

void Push() { tp++; f[tp]=oo; }

int Find(int le,int ri,int x)
{
	for ( ; le+1<ri; )
	{
		int mid=(le+ri)>>1;

		if (x<=f[mid]) ri=mid; else le=mid; 
	}

	return ri;
}//二分查找

int a[100100];

int main()
{
	int n; scanf("%d",&n);

	for (int i=0; i<n; i++) scanf("%d",&a[i]);

	f[0]=-oo; Push();

	for (int i=0; i<n; i++)
	{
		int p=Find(0,tp,a[i]);

	f[p]=a[i];  //更新

		if (p==tp) Push();
	}

	printf("%d",tp-1);

	return 0;
}

lower_bound(a+be,a+ed,x) 返回地址,即&a[ans],加*取出,减&a[be]得到位置。
f[i]表示长度为i的序列中,结尾最小是几。


CC 原创文章采用CC BY-NC-SA 4.0协议进行许可,转载请注明:“转载自:最长上升子序列