Returns the and of the largest interconnected subarray in a two-dimensional integer array

Source: Internet
Author: User

Title: Returns the and of the largest interconnected subarray in a two-dimensional integer array

Pair PARTNER: Ding

Blog Address: http://www.cnblogs.com/dgj1314/

Blog name: With sincerity

1 problem: Returns the number of the largest interconnected subarray in a two-dimensional integer array

2 idea: To decompose the two-dimensional array of n*m, decomposed into n one-dimensional arrays, and then the largest subarray of n one-dimensional arrays and note the subscript of the largest one-dimensional sub-array of each row, such as 2-5, which is divided into two cases the first is the largest sub-array between rows is connected, such as the first row is 2-5, the second row is 3-6, This is directly added to 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 Code:

#include <iostream>using namespace Std;int zuida (int n,int a[],int *sm,int *mm); void main () {int m,n,i,j,sm,mm,t2;    int Sum,max;    int up[100],down[100],t[100];    int a[100][100],b[100];    cout<< "Enter rows for two-dimensional arrays" <<endl;     cin>>m;     cout<< "Enter rows 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,&AMP;SM,&AMP;MM);                                           Up[i]=sma;        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]; Discriminant independent Positive}} cout<<t2<<endl; }int zuida (int n,int a[],int *sm,iNT *mm) {int b[100]={0};    int i,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 }} return max1;}
4 Summary: This time we have spent more than the last one times to finish at last.

Returns the and of the largest interconnected subarray in a two-dimensional integer array

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.