码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1836 Alignment 【LIS(二分)+枚举】

时间:2014-06-09 23:29:00      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:lis

a1,a2,a3,a4,a5,a6...an

对ai求出a1到ai的lis,ai+1到an的lds

取所有ai对应的lis+lds最大值

输出n-lis-lds

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
int n;
double num[1111];
double up[1111];
double down[1111];
const int inf=(1<<31)-1;
int b1(double ss,int Left,int Right)
{
	int L=Left;int R=Right;
	int mid;
	while(L<R)
	{
		mid=(L+R)>>1;
		if(up[mid]>ss)
		{
			R=mid;
			mid=(L+R)>>1;
		}
		else if(up[mid]==ss)
		{
			return mid;
		}
		else
		{
			L=mid+1;
			mid=(L+R)>>1;
		}
	}
	return R;
}
int b2(double ss,int Left,int Right)
{
	int L=Left;int R=Right;
	int mid;
	while(L<R)
	{
		mid=(L+R)>>1;
		if(down[mid]<ss)
		{
			R=mid;
			mid=(L+R)>>1;
		}
		else if(down[mid]==ss)
		{
			return mid;
		}
		else
		{
			L=mid+1;
			mid=(L+R)>>1;
		}
	}
	return R;
}
int main()
{
	#ifndef ONLINE_JUDGE
		freopen("G:/1.txt","r",stdin);
		freopen("G:/2.txt","w",stdout);
	#endif
	int ans=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%lf",&num[i]);
	for(int i=1;i<=n;i++)
	{
	    memset(up,0,sizeof(up));
	    memset(down,0,sizeof(down));
		int lisnum=1;
		up[0]=-1;
		for(int j=1;j<=i;j++)
		{
			int index=b1(num[j],0,lisnum);
			if(index==lisnum)
				lisnum++;
			up[index]=num[j];
		}

		int ldsnum=1;
		down[0]=inf;
		for(int j=i+1;j<=n;j++)
		{
			int index=b2(num[j],0,ldsnum);
			if(index==ldsnum)
				ldsnum++;
			down[index]=num[j];
		}
		lisnum--;ldsnum--;
		//cout<<lisnum-1<<' '<<ldsnum-1<<endl;
		ans=max(ans,lisnum+ldsnum);
	}
	printf("%d\n",n-ans);
}


 

POJ1836 Alignment 【LIS(二分)+枚举】,布布扣,bubuko.com

POJ1836 Alignment 【LIS(二分)+枚举】

标签:lis

原文地址:http://blog.csdn.net/u011775691/article/details/28613191

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!