1. Title: Returns the and of the largest interconnected subarray in a two-dimensional integer array. Requirements: Enter a two-dimensional shape array with positive and negative numbers in the array. The maximum value for the and of all sub-arrays. Requires a time complexity of O (n). 2. Design ideas for n*m two-dimensional array decomposition, decomposition into n one-dimensional array, and then first to find the largest subarray of n one-dimensional array, and note the maximum one-dimensional sub-array of each row subscript, such as 2-5, which is divided into two cases the first is the largest sub-array between rows, such as the first row is 2-5, The second line is 3-6, which is added directly on the line. The second is not connected, such as the first row is 2-5, the second row is 6-7, this time the largest sub-array of each row as a whole, and then make each of the largest array of blocks connected, so that the minimum cost of its connection. Finally, we can find out the maximum number of interconnected sub-arrays. 3. Source code
#include <iostream>using namespacestd;intZuida (intNintA[],int*SM,int*mm);voidMain () {intm, N, I, J, SM, MM, T2; intsum, Max; intup[ -], down[ -], t[ -]; inta[ -][ -], b[ -]; cout<<"Enter rows for two-dimensional arrays"<<Endl; CIN>>m; cout<<"Enter columns for two-dimensional arrays"<<Endl; CIN>>N; for(i =0; i<m; i++) { for(j =0; j<n; J + +) {cin>>A[i][j]; } } for(i =0; i<m; i++) { for(j =0; j<n; J + +) {B[j]=A[i][j]; } Sum= Zuida (n, B, &SM, &mm); Up[i]=sum; Down[i]=mm; T[i]=sum; } T2= t[0]; for(i =0; i +1<m; i++) { if(Up[i] <= down[i +1] && Down[i] >= up[i +1]) {T2+ = T[i +1]; } for(j = up[i]; J<up[i +1]; J + +) { if(A[i +1][j]>0) T2 + = A[i +1][J];//distinguishing independent positive numbers}} cout<< T2 <<Endl;}intZuida (intNintA[],int*SM,int*mm) { intb[ -] = {0 }; intI, sum1 =0, Max1 =0; for(i =0; i<n; i++) { if(sum1<0) {sum1=A[i]; } Else{sum1= Sum1 +A[i]; } B[i]=sum1; } max1= b[0]; for(i =0; i<n; i++) { if(max1<B[i]) {Max1=B[i]; *MM =i; } } for(i = *mm; I >=0; i--) { if(B[i] = =A[i]) { *SM =i; Break; } } returnmax1;}
4 procedures
5. Summary
The program was not written at the time, and then it was gone. This practice is very incorrect, in the future encounter problems and difficulties to learn to solve, rather than give up.
Maximum Connectivity sub-array