1065: Chorus formation (longest ascent, descending sub-sequence)

Source: Internet
Author: User
Tags time limit

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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.