题目描述
NN 位同学站成一排,音乐老师要请其中的( N-KN−K )位同学出列,使得剩下的 KK 位同学排成合唱队形。
合唱队形是指这样的一种队形:设K位同学从左到右依次编号为 1,2,…,K1,2,…,K ,他们的身高分别为 T_1,T_2,…,T_KT
1
,T
2
,…,T
K
, 则他们的身高满足 T_1<...<T_i>T_{i+1}>…>T_K(1 le i le K)T
1
<...<T
i
>T
i+1
>…>T
K
(1≤i≤K) 。
你的任务是,已知所有N位同学的身高,计算最少需要几位同学出列,可以使得剩下的同学排成合唱队形。
输入输出格式
输入格式:
共二行。
第一行是一个整数 N(2 le N le 100)N(2≤N≤100) ,表示同学的总数。
第二行有 nn 个整数,用空格分隔,第 ii 个整数 T_i(130 le T_i le 230)T
i
(130≤T
i
≤230) 是第 ii 位同学的身高(厘米)。
输出格式:
一个整数,最少需要几位同学出列。
输入输出样例
输入样例#1:
8
186 186 150 200 160 130 197 220
输出样例#1:
4
说明
对于50%的数据,保证有 n le 20n≤20 ;
对于全部的数据,保证有 n le 100n≤100 。
做n次LIS即可
欸,准确的讲是最长下降子序列,都一样了
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
inline int rd(){
int ret=0,f=1;char c;
while(c=getchar(),!isdigit(c))f=c=='-'?-1:1;
while(isdigit(c))ret=ret*10+c-'0',c=getchar();
return ret*f;
}
const int MAXN=1000;
int f[MAXN];
int val[MAXN],a[MAXN];
int n;
int main(){
n=rd();
for(int i=1;i<=n;i++) a[i]=rd();
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++) f[j]=0;
int mx=0;
for(int j=i;j<=n;j++){
for(int k=i;k<j;k++){
if(a[k]>a[j]) f[j]=max(f[j],f[k]+1),mx=max(f[j],mx);
}
}
val[i]+=mx;
mx=0;
for(int j=i;j>=1;j--){
for(int k=i;k>j;k--){
if(a[k]>a[j]) f[j]=max(f[j],f[k]+1),mx=max(f[j],mx);
}
}
val[i]+=mx;
val[i]++;
}
int ans=-1;
for(int i=1;i<=n;i++) ans=max(ans,val[i]);
cout<<n-ans<<endl;
}