题意不难理解,这里就不说了。
思路就是开两个数组fro[n],back[n],先从头到尾求一遍LIS,记录在fro[n]中,再从尾到头求一遍LIS,记录在back[n]中,最后遍历fro[n],back[n],符合要求的最长队列就是max(fro[i]+back[i]),最终答案就是n减去这个数了。
注意:这里求LIS是要用朴素的n2算法,才可以去解这道题,否则可能不行。
#include<queue>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=1e2+5;
int fro[maxn],back[maxn],h[maxn]; //fro[i]表示以h[i]为结尾的最长递增子序列,back[i]同理,只是扫描顺序反了而已
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>h[i];
for(int i=1;i<=n;i++)
{
int temp=-inf;
for(int j=i-1;j>=1;j--)
{
if(h[j]<h[i])
{
if(temp<fro[j])
temp=fro[j];
}
}
if(temp==-inf)
fro[i]=1;
else fro[i]=temp+1;
}
for(int i=n;i>=1;i--)
{
int temp=-inf;
for(int j=i+1;j<=n;j++)
{
if(h[j]<h[i])
{
if(temp<back[j])
temp=back[j];
}
}
if(temp==-inf)
back[i]=1;
else back[i]=temp+1;
}
int ans=0;
for(int i=1;i<=n;i++)
ans=max(ans,fro[i]+back[i]); //根据fro,back数组的定义,不难想出这样做的对的
cout<<n-ans+1<<endl; //最终还要加1,因为h[i]这个人被多算了一遍
return 0;
}