Title: The and of the largest sub-arrays in an integer array
Requirements:
- Enter an array of shapes with positive and negative numbers in the array.
- One or more consecutive integers in an array make up a sub-array, each of which has a and.
- If the array a[0] ... A[j-1] next to each other, allowing a[i-1], ... A[n-1],a[0] ... A[J-1] and the largest.
- Returns the position of the largest subarray at the same time. The maximum value for the and of all sub-arrays.
One more requirement for this mission is that the array is ring-shaped,
which can be from a[i-1], ... A[n-1],a[0], a[1] Such an array is also wide, then the first thought is the loop linked list, and then control the sum of the sub-array length, but, where the first element of the list is determined ... With DP words termination conditions how to set ...
So I feel that this method may not be very good to go, so, think of since it is a ring array, that sub-array must have requirements, if the array elements all the sum is positive,
So the sum of the sums of the subarray is not +∞, so it can be ...
, the length of the sub-array must be less than or equal to the original array, then a new array of two array arr is bound to contain all the sub-arrays.
And then the table to find all the sub-array of the and, and then Max, this is not extremely easy, hurriedly yards up
1#include <iostream>2#include <vector>3 using namespacestd;4 #defineMax_num 1005 intMainintargcChar*argv[])6 {7 inta[]={ the, the,-565,9465,- the, One, the,9400};8vector<int> Arr (a,a+ (int)(sizeof(a)/sizeof(int)));9 Arr.insert (Arr.end (), Arr.begin (), Arr.end ());Ten intarrtemp[max_num][max_num]={0};//table arrtemp[i][j] for the sum of the sequence of records is represented as a subarray of the sum of [i,j] elements in arr One for(inti =0; I < (int) (Arr.size ()/2); i++) A { - for(intj =0;j< (int) (Arr.size ()); J + +) - { the if((i <= J) && (J < i + (int) (Arr.size ()/2)))//I<=j Control Element order, J <i+length Control sub-series length - { - for(intK = i; K <= J; k++) - { +ARRTEMP[I][J] + =Arr[k]; - } + } A } at } - - intMaxtemp = arrtemp[0][0];//the temporary maximum value is set to Arr[0][0] that is arr[0] - intStart =0; - intEnd =0; - for(inti =0; I < (int) (Arr.size ()/2); i++) in { - for(intj =0; J < (int) (Arr.size ()); J + +) to { + if((i <= J) && (J < (int) (I+arr.size ()/2)))//I<=j Control Element Order, J<i+length control sub-series length - { the if(Arrtemp[i][j] >maxtemp) * { $Maxtemp =Arrtemp[i][j];Panax NotoginsengStart =i; -End =J; the } + } A } the } + -cout <<"sum:"<< maxtemp <<"Start:"<< Start <<"End:"<<end% (arr.size ()/2) <<Endl; $ return 0; $}
(i <= J) && (J < (int) (I+arr.size ()/2) is the sum of the control elements, such as an array with 4 elements, then the contents of the sum table can only be filled to
x o o o o x x x
x x o o o x x
x x x o o o x
Position in O, other locations cannot be the sum of sub-arrays.
also drunk.
Date |
Lectures |
Programming |
Reading |
Look at the code |
Write a blog |
Summarize |
Monday |
120 |
120 |
|
150 |
|
390 |
Tuesday |
|
60 |
|
120 |
|
180 |
Wednesday |
|
|
|
90 |
|
90 |
Thursday |
120 |
120 |
|
60 |
|
300 |
Friday |
|
|
|
120 |
|
120 |
Saturday |
|
60 |
|
|
|
60 |
Sunday |
|
240 |
|
|
90 |
330 |
Week Summary |
240 |
600 |
0 |
540 |
90 |
1470 |
Defect Logging
Date |
Number |
Type |
Introduction phase |
Exclusion phase |
Repair time |
Fixing defects |
3/27 |
1 |
Stack Overflow |
Coding |
Debugging |
20min |
|
My group members: Liu Wei
To find the maximum number of sub-arrays 02