代码如下:
//P1020 [NOIP1999 普及组] 导弹拦截
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int a[N],len;//len用来记录序列长度
int sta[N],top;//手 写 栈
int sta1[N],top1;//手 写 栈 2
int main()
{
for(len=1;cin>>a[len];++len){}
sta[++top]=a[1];
for(int i=2;i<len;++i)//最长不上升子序列
{
if(a[i]<=sta[top])sta[++top]=a[i];
else//找到第一个小于等于a[i]的数
{
int l=1,r=top;
while(l<r)
{
int mid=(l+r)/2;
if(sta[mid]>a[i])l=mid+1;
else r=mid;
}
if(sta[l]==a[i])sta[l+1]=a[i];//等于的时候要下一个元素赋值,不然答案少一
else sta[l]=a[i];
}
//for(int i=1;i<=top;++i)cout<<sta[i]<<' ';
//cout<<" "<<i<<endl;
}
sta1[++top1]=a[1];
for(int i=2;i<len;++i)//最长上升子序列
{
if(a[i]>sta1[top1])sta1[++top1]=a[i];
else//找到第一个大于等于a[i]的数
{
int l=1,r=top1;
while(l<r)
{
int mid=(l+r)/2;
if(sta1[mid]>=a[i])r=mid;
else l=mid+1;
}
sta1[l]=a[i];
}
//for(int i=1;i<=top1;++i)cout<<sta1[i]<<' ';
//cout<<endl;
}
cout<<top<<endl<<top1;
return 0;
}
应该是找最长不上升子序列时的二分查找出了问题,但问题具体出在哪里?爆肝调了好久都挑不出来,望大佬解答
感谢指教!