导弹拦截题解

使用STL中的upper_bound和lower_bound代替二分操作

对于问一求最长不上升子序列,问二求最长上升子序列(为什么问二是这样是我也不知道)
巨坑的点,对于问一,f数组中的数据是下降(从大到小排序)的,必须添加greater(),二分的原则是排好序啊!!!
终于AC代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;
int a[100005],p,f[100005],k=0;

int main()
{
	
	while(cin>>a[p])
	{ 
		p++;
	}
	f[0]=a[0];
	for(int i=1;i<p;i++)
	{
		if(a[i]<=f[k])
		{
			f[++k]=a[i];
		}
		else
		{
			int x=upper_bound(f,f+k,a[i],greater<int>())-f;
			f[x]=a[i];
		}
	}	
	cout<<k+1<<endl;
	k=0;
	memset(f,0,sizeof(f));
	f[0]=a[0];
	for(int i=1;i<p;i++)
	{
		if(a[i]>f[k])
		{
			f[++k]=a[i];
		}
		else
		{
			int x=lower_bound(f,f+k,a[i])-f;
			f[x]=a[i];
		}
	}	
	cout<<k+1<<endl;
	
	return 0;	
} 
原文地址:https://www.cnblogs.com/huaruoji/p/12951898.html