POJ2533

int bisert(int c[1000000],int j,int ai)
{
	int low=1;
	int high=j;
	int mid;
	while(low<=high)
	{
		mid=(high+low)/2;
		if(c[mid]<ai){
			if(c[mid+1]>ai)
				return mid;
			else
				low=mid+1;
			}
		else{
			if(c[mid-1]<ai)
				return mid-1;
			else
				high=mid-1;
		}
	}
	return low;
}
 #include<stdio.h>
int a[1000000];
int c[1000000];
int main(){
	int length;
	scanf("%d",&length);
	int i;
	for(i=0;i<length;i++)
		scanf("%d",&a[i]);
	c[1]=a[0];//c[i] stands for the smallest ending number of all increasing sequences with length i
	int j=1;//j is the length of the longest increasing sequence currently
	int k;
	for(i=1;i<length;i++){
		if(c[1]>a[i]){//set a[i] as the smallest ending number of all increasing sequences with length 1
			c[1]=a[i];
		}
		else if(c[j]<a[i]){//a[i] can be put into the extended increasing sequence of lenght j+1
			j++;
			c[j]=a[i];
		}
		else{//a[i] can be put into the extended increasing sequence of lenght k+1 for some k, such that 1<k<j
			k=bisert(c,j,a[i]);
			c[k+1]=a[i];
		}
		}
		printf("%d\n",j);
		return 0;
		}

问题详见poj2533。

如果这里数组的长度要求100,0000,那么O(n^2)的算法就不能用了。上面的算法只用了O(nlogn)时间。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值