1508 question: [Blue Bridge Cup] [algorithm improves VIP] and the maximum subsequence
Time limit: 1Sec Memory Limit: 128MB submit: 487 Resolution: 160
Title Description
For a given sequence length N is an integer A, which defines the "sequence" of be: A stretch of consecutive non-null elements (integer). You have to complete the task, all possible sequences, find a sequence, the sequence of all the elements and is the largest (in comparison with all other sub-sequence). This procedure requires you to maximum output.
Entry
The first line of the input file contains an integer N, the second line contains N integers, represents A.
Wherein
. 1 <= N <= 100000
-10000 <= A [I] <= 10000
Wherein
. 1 <= N <= 100000
-10000 <= A [I] <= 10000
Export
The output contains only an integer representing you figure out the answer.
Sample input
5 3 -2 3 -5 4
Sample Output
4
#include "stdio.h" #include "stdlib.h" #include "iostream" #include "string.h" #include "math.h" #include "algorithm" #include "queue" #include "vector" #include "map" #define Mod 1000000007 using namespace std; int main(){ int n; scanf("%d", &n); int A[n]; for(int i = 0; i < n; i++){ scanf("%d", &A[i]); } int dp[n + 1]; dp[0] = 0; int t = INT_MIN; for(int i = 1; i <= n; i++){ dp[i] = max(dp[i - 1] + A[i - 1], A[i - 1]); //Here is dp [i] = max (dp [i - 1] + A [i - 1], A [i - 1]); annotations // DP [i] is an i-value generally continuous array of item A and // if a is added to the array of items not found in the x-a [x] plus large case it will certainly not be the largest sequence DP [x +. 1] = a [x]; // mean At this time has been interrupted, again looking onward sequence T = max (T, DP [I]); } // COUT << T << endl; for ( int I = . 1 ; I <= n-; I ++ ) { COUT DP << [I] << " " ; } return 0 ; }