Queue-type -- rqnoj_26 -- longest monotonic subsequence

Source: Internet
Author: User

Address: http://www.rqnoj.cn/Problem_26.html

Question: team-based

Question No.: 26

Description

N students stood in a row, and the music teacher asked the (N-K) students to make the remaining K students lined up.
A queue is a formation in which K students are numbered 1, 2, and so on from left to right ..., K. Their heights are T1, T2 ,..., TK, then their height meets t1 <... <Ti> Ti + 1>...> TK (1 <= I <= K ).
Your task is to know the height of all N students. The calculation requires at least a few students to make the remaining students form a queue.

Input Format

The first line is an integer N (2 <=n <= 100), indicating the total number of students. The first line contains N integers separated by spaces. the I-th integer Ti (130 <= Ti <= 230) is the height (cm) of the I-th student ).

Output Format

The output includes a row. This row contains only one integer, that is, at least a few students are required for the column.

Sample Input

8
186 186 150 200 160 130 197

Sample output

4

Analyze the meaning of the question first. The question indicates that a student is expected to participate in a chorus competition and has been determined. Now we need to adjust the formation. So that a person is the highest, the height on the left is increasing, and the height on the right is decreasing. For convenience, two sides are not required to be symmetric. Now, if you want to kick some people in the current formation, the formation meets the requirements, and you need to know the minimum number of people to kick. My idea for solving this problem is to use F1 [I] to represent the largest number of people standing in the ascending formation, that is, the longest ascending subsequence. Then, F2 [J] is used to indicate the maximum number of students standing in descending order. Then, from the first to the last traversal, find the two and the largest, and then subtract the number of the two, that is, the maximum number of people standing in the queue. The sum of the total number minus the maximum number of people is the minimum number of people to be kicked out.

State transition equation:

F1 [I] = max (F1 [J] + 1) 0 <= j <I <= n I from 0 to J & STU [J] <STU [I]

F2 [I] = max (F2 [J] + 1) 1 <= I <j <= n + 1 I from N to 1 &&
STU [J] <STU [I]

Next, we should pay attention to the problem of Initial Value assignment. For F1 and F2, because DP is started from scratch, F1 [0] = 0, STU [0] =-1, STU [n + 1] =-1 F2 [0] = 0; because when F2 is obtained, it is equivalent to reverse-finding the longest incrementing subsequence. If it is positive, it is decreasing. And you must pay attention to it. This ensures that F2 [J] StoresUse J as the headerThe maximum number of descending subsequences of, the following code:

# Include <iostream> using namespace STD; # define Max 110int main () {int F1 [Max], F2 [Max]; int ans = 0; int STU [Max]; int N; while (CIN> N & N) {int I, j; for (I = 1; I <= N; I ++) {CIN> STU [I]; F1 [I] = F2 [I] = 0;} F1 [0] = F2 [0] = 0; f1 [n + 1] = F2 [n + 1] = 0; // first obtain the longest incrementing subsequence STU [0] =-1; for (I = 1; I <= N; I ++) {for (j = I-1; j> = 0; j --) {If (STU [J] <STU [I] & F1 [J] + 1> F1 [I]) F1 [I] = F1 [J] + 1 ;}} // obtain the longest descending subsequence STU [n + 1] =-1; for (I = N; I> = 1; I --) {for (j = I + 1; j <= n + 1; j ++) {If (STU [J] <STU [I] & F2 [J] + 1> F2 [I]) f2 [I] = F2 [J] + 1 ;}// find the most appropriate int ans = 0; for (I = 1; I <= N; I ++) {If (ANS <F1 [I] + F2 [I]) ans = F1 [I] + F2 [I];} cout <n-ans + 1 <Endl;} 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.