Maximum Ascending sequence and

Source: Internet
Author: User

  A number of sequence bi, when B1 < B2 < ... < BS, we call this sequence ascending. For a given sequence (A1, A2, ..., an), we can get some ascending sub-sequences (AI1, AI2, ..., AiK), here 1 <= i1 < I2 < ... < IK <= N. For example, for sequences (1, 7, 3, 5, 9, 4, 8), there are some ascending sub-sequences of it, such as (1, 7), (3, 4, 8) and so on. The sequence and maximum of these subsequence are 18, and are the and of the subsequence (1, 3, 5, 9).

Your task is to find the maximum ascending subsequence for a given sequence. Note that the longest ascending subsequence and not necessarily the largest ascending subsequence of the sequence (100, 1, 2, 3) and 100, while the longest ascending sequence is (1, 2, 3).

This is also a classic DP problem, we can use an array of sum[i] to record the maximum ascending subsequence from 1 to I and. Then Sum[i]=max (Sum[j]) +a[i] {1<=j<i}, and for any one J satisfies a[i]>a[j]. Only in this way can the sequence be guaranteed to rise. Comparing the length of the longest ascending subsequence, we can find that the two DP problem ideas are exactly the same, their code can be said to be the same, the difference between the code is the difference between the question and the evaluation is different?

The code is as follows:

  

#include <cstdio>using namespacestd;#defineMax (x, y) (x > Y x:y)__int64 sum[1005];inta[1005];voidLIS (intN) {     for(intI=1; i<=n; i++) {__int64 max=0;  for(intj=1; j<i; J + +)        {            if(A[i] >A[j]) {Max=Max (max, sum[j]); }} Sum[i]= A[i] +Max; //printf ("sum[%d]=%d+%i64d\n", I,a[i],max);    }}intMain () {intN;  while(~SCANF ("%d", &n) &&N) { for(intI=1; i<=n; i++) scanf ("%d", A +i); LIS (n); //find each ascent and Sum[i]//find out the biggest and__int64 ans=0;  for(intI=1; i<=n; i++) ans=Max (ans, sum[i]); printf ("%i64d\n", ans); }    return 0;}
View Code

Maximum Ascending sequence and

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.