And largest subsequences-Dynamic Planning, sequence-Planning
Problem description for A given integer sequence A whose length is N, its "subsequence" is defined as A consecutive element (integer) that is not null in ). The task you want to complete is to find a sub-sequence among all possible sub-sequences. The sum of all elements in the sub-sequence is the largest (compared with all other sub-sequences ). The program requires you to output the maximum value. The first line of the input file contains an integer N, and the second line contains N integers, indicating.
Where
1 <= N <= 100000
-10000 <= A [I] <= 10000 the output format contains only one integer, indicating the answer you calculated. Sample input 53-2 3-5 4 sample output 4
I think there are two solutions to this problem: Simulation + greedy and dynamic planning. For the first one, I think the amount of code is large, so I chose the second method to solve this problem. In fact, the state transition equation of this question is well determined.D [I] = max (d [I-1] + a [I], 0); D [I] represents the sum of the largest continuous subsequences at the position I, and a [I] represents the value of a layer loop at the position I, which is very simple.
# Include <stdio. h ># include <algorithm> using namespace std; int a [100005]; int d [100005]; int main () {int n; scanf ("% d ", & n); for (int I = 1; I <= n; I ++) {scanf ("% d", & a [I]);} int maxn = 0; for (int I = 1; I <= n; I ++) {d [I] = max (d [I-1] + a [I], 0); if (d [I]> maxn) maxn = d [I];} printf ("% d \ n", maxn); return 0 ;}