The longest ascending sequence and

Source: Internet
Author: User

Topic website: http://ac.jobdu.com/problem.php?pid=1480
Title Description:

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).

Input:

The input contains multiple sets of test data.
Each set of test data consists of two lines. The first line is the length of the sequence n (1 <= n <= 1000). The second line gives the n integers in the sequence, with values ranging from 0 to 10000 (possibly duplicates).

Output:

For each set of test data, output its maximum ascending subsequence and.

Sample input:
71 7 3 5 9 4 8
Sample output:
18
Source:
2012 Peking University computer Research Life Test real problem
#include <iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespacestd;inta[1005];intsum[1005];intMain () {intn,s,f;  while(cin>>N) { for(intI=1; i<=n;i++) Cin>>A[i]; sum[0]=0; sum[1]=a[1]; S=sum[1];  for(intI=2; i<=n;i++)        {            intmaxn=0;  for(intj=1; j<=i-1; j + +)            {                if(a[j]<a[i]&&sum[j]>MAXN) {MAXN=Sum[j]; }} Sum[i]=maxn+A[i]; if(sum[i]>s) s=Sum[i]; } cout<<s<<Endl; }    return 0;}

The longest 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.