Original title link
1065: [VIDEO] Getting started with dynamic planning (one-dimensional side push 3: Chorus formation)
time limit: 1 Sec memory limit: MB
Commit: 339 FIX: 168
[Submit] [status] [discussion]
Topic description
"title"
N students stand in a row, the music teacher to please one of the students (N-K) out, so that the rest of the K-students lined up in chorus formation. The
Chorus formation refers to such a formation: Set K-students from left to right numbered 1,2...,k,
their height is t1,t2,...,tk, then their height meet T1 < T2 ...< Ti > Ti+1 > ... >tk (1< ; =i<=k).
Your task is to know the height of all n students, calculate the minimum number of students need to dequeue, you can make the rest of the students lined up in chorus formation.
Input Format
The first line is an integer N (2<=n<=100) that represents the total number of classmates.
down n integers, separated by spaces, and the first integer Ti (130<=ti<=230) is the height of the first classmate (CM). The
Output format
includes a row that contains only an integer, which requires a minimum of several classmates to dequeue.
Sample Input
8
186 186 197
"Sample Output"
4
First the longest ascending subsequence, and then the longest descent subsequence, the current position of two number and maximum, the lowest person out of the queue.
#include <iostream> #include <algorithm> using namespace std; int num[1005],ans[
1005],ANS2[1005];
int n;
int main () {cin>>n;
for (int i=1;i<=n;i++) {cin>>num[i];
Ans2[i]=ans[i]=1; } for (int i=2;i<=n;i++)//Longest ascending subsequence {for (int j=i-1;j>=ans[i];j--) {if (num[i]&
Gt;num[j]) {Ans[i]=max (ans[j]+1,ans[i]);
}}} for (int i=n-1;i>=1;i--)//Longest descent subsequence {for (int j=i+1;j<=n;j++) {
if (Num[i]>num[j]) Ans2[i]=max (ans2[i],ans2[j]+1);
}} for (int i=1;i<=n;i++) ans[i]+=ans2[i];
Cout<<n-*max_element (ans+1,ans+n+1) +1;
return 0; }