繼續繼續繼續刷題之路。。
題目描述 Description
N位同學站成一排,音樂老師要請其中的(N-K)位同學出列,使得剩下的K位同學排成合唱隊形。
合唱隊形是指這樣的一種隊形:設K位同學從左到右依次編號爲1,2…,K,他們的身高分別爲T1,T2,…,TK, 則他們的身高滿足T1<...<Ti>Ti+1>…>TK(1<=i<=K)。
你的任務是,已知所有N位同學的身高,計算最少需要幾位同學出列,可以使得剩下的同學排成合唱隊形。
輸入描述 Input Description
輸入文件chorus.in的第一行是一個整數N(2<=N<=100),表示同學的總數。第一行有n個整數,用空格分隔,第i個整數Ti(130<=Ti<=230)是第i位同學的身高(釐米)。
輸出描述 Output Description
輸出文件chorus.out包括一行,這一行只包含一個整數,就是最少需要幾位同學出列。
樣例輸入 Sample Input
8
186 186 150 200 160 130 197 220
樣例輸出 Sample Output
4
數據範圍及提示 Data Size & Hint
對於50%的數據,保證有n<=20;
對於全部的數據,保證有n<=100。
已知讓求最小的刪除人的數目使隊列先增後減
那麼,
正反雙向做兩次最長上升子序列
枚舉拐點。
當前點的兩側最大上升子序列和最大的點即爲最優拐點
嗯。
代碼::
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cstdlib>
#include<string>
#include<bitset>
#include<iomanip>
#include<deque>
#define INF 1000000000
#define fi first
#define se second
#define N 100005
#define P 1000000007
#define debug(x) cerr<<#x<<"="<<x<<endl
#define MP(x,y) make_pair(x,y)
using namespace std;
int n,m,a[101],f[101],d[101];
inline int get_num()
{
int num = 0;
char c;
bool flag = false;
while ((c = getchar()) == ' ' || c == '\n' || c == '\r');
if (c == '-') flag = true;
else num = c - '0';
while (isdigit(c = getchar()))
num = num * 10 + c - '0';
return (flag ? -1 : 1) * num;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=n;i++) f[i]=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<i;j++)
{
if(a[i]>a[j])
{
f[i]=max(f[i],f[j]+1);
}
}
}
for(int i=1;i<=n;i++) d[i]=1;
for(int i=n;i>=1;i--)
{
for(int j=n;j>i;j--)
{
if(a[i]>a[j])
{
d[i]=max(d[i],d[j]+1);
}
}
}
int ans=2147483647;
for(int i=1;i<=n;i++)
{
ans=min(ans,n-d[i]-f[i]+1);
}
cout<<ans;
}
嗚啦啦啦啦啦~