Algorithm: Longest ascending descent sub-sequence

Source: Internet
Author: User

Problem

The number of X (x>=0) is taken from the given number of N. Make the remaining number the most of the following properties.
A1 < A2 < A3 <, .... At > at+1 >at+2 > ... > As
Ask at least a few numbers to be drained. This series will only have the above properties.


Graphic

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvenh5odyxmte0/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "style=" WIDTH:589PX; height:431px ">

Code

#include "stdafx.h" const int max=105;int down[max],up[max];int h[max],n;void get_up () {int i,tmp,j;for (i=0;i<n;i++) {tmp=1;for (j=i-1;j>=0;j--) {if (h[i]>h[j]&&up[j]+1>tmp) tmp=up[j]+1;} Up[i]=tmp;}} void Get_down () {int i,j,tmp;for (i=n-1;i>=0;i--) {tmp=1;for (j=i+1;j<n;j++) {if (h[i]>h[j]&&down[j]+ 1>tmp) tmp=down[j]+1;} Down[i]=tmp;}} int main () {int i,max;while (scanf ("%d", &n)!=eof) {for (i=0;i<n;i++) scanf ("%d", &h[i]); get_up (); Get_down () ; Max=0;for (i=0;i<n;i++) {if (Up[i]+down[i]-1>max) max=up[i]+down[i]-1;} printf ("%d\n", N-max);} return 0;}



Algorithm: Longest ascending descent sub-sequence

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.