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
This problem is not a problem, just use a sum[] to record the maximum rise of each position, and each back to find the previous one is smaller than the current element and the largest sum[] added to find the maximum rise of the current position.
But at first I took it for granted that the nearest small value of the current element is the element being asked, and an array is used to record it, causing the problem to be too complex.
Then finally the simplest way to do it out, but note that it is strictly ascending sub-sequence.
1#include <cstdio>2#include <cstdlib>3#include <cstring>4 #defineMAX 10055 intSum[max];6 intNum[max];7 //1 2 3 4 5 6 78 //1 7 3 5 9 4 89 //0 1 1 3 4 3 6Ten //1 8 4 9 7 One //0 5 2 3 1 0 2 4 5 8 A //5 4 8 2 5 7 5 6 - - intMainintargcChar Const*argv[]) the { - intN; - while(SCANF ("%d", &n)! =EOF) { - intMax =0; + for(inti =1; I <= N; i++) { -scanf"%d",&num[i]); + } Asum[1] = num[1]; at for(inti =1; I <=n; i++) { - intMaxtemp =Num[i]; - for(intj = i-1; J >=1; j--) { - if(Num[i] >Num[j]) { - inttemp = Sum[j] +Num[i]; - if(Maxtemp <temp) { inMaxtemp =temp; - } to } + } -Sum[i] =maxtemp; the if(Max <maxtemp) { *Max =maxtemp; $ }Panax Notoginseng - } the /*for (int i = 1; I <= n; i++) { + printf ("%d\t", Sum[i]); A }*/ theprintf"%d\n", max); + } - //System ("pause"); $ return 0; $}
Nine degrees OJ 1480