poj 1187

//读了半大天题终于发现就是LIS问题
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=100000;
const int inf=1<<30;
int s[maxn],r[maxn],d[maxn],g[maxn];
int n;
int low_bound(int x)
{
	int high=n,low=0,mid;
	while(high-low>0)
	{
		mid=low+(high-low)/2;
		if(g[mid]>=x) high=mid;
		else low=mid+1;
	}
	return high;
}
int main()
{
	int tot=0;
	while(cin>>s[0]&&s[0]!=-1)
	{
		tot++;
		int tem,i;
		n=1;
		while(cin>>tem&&tem!=-1) s[n++]=tem;
		for(i=0;i<n;i++)
		{
			r[i]=s[n-1-i];
			g[i]=inf;
		}
		int k,max=-1;
		for(i=0;i<n;i++)
		{
			k=low_bound(r[i]);
			d[i]=k+1;//以i结尾的最大上升子序列的个体数
			g[k]=r[i];//更新g[k]
			if(max<d[i]) max=d[i];
		}
		printf("Test #%d:\n",tot);
		printf("  maximum possible interceptions: %d\n",max);
		printf("\n");
	}
	return 0;
}


原文地址:https://www.cnblogs.com/lj030/p/3002292.html