Maximum sub-array of previous test questions

Source: Internet
Author: User

Problem description Given a n*m matrix A, a non-empty matrix in a, so that the element in this sub-matrix and the largest.

Wherein, A's sub-matrix refers to a row and column in a continuous block. Input format the first line of input contains two integers n, m, each representing the number of rows and columns of matrix A.
The next n rows, m integers per line, represent matrix A. The output format outputs a row that contains an integer representing the elements in the largest sub-matrix in a. Sample Input 3 3
-1-4 3
3 4-1
-5-2 8 Sample Output 10 Sample Description take the last column, and 10. Data size and conventions for 50% of data, 1<=n, m<=50;
For 100% of data, 1<=n, the absolute value of each element in M<=500,a does not exceed 5000. Idea: Save the and (bottom-up) of each row i~j column with b[n+1], then the maximum subsequence and, last update maxsum
#include <stdio.h>#include<stdlib.h>#include<string.h>#defineN 501intA[n][n];intn,m;intSub_sum (int*b) {    intS,max; S= max = b[1]; inti;  for(i=2; i<=n;i++) {s+=B[i]; if(S >max) Max=s; if(S <0) s=0; }    returnMax;}intMain () {inti,j,t,maxsum,s; int*b; scanf ("%d%d",&n,&m); b= (int*)malloc(sizeof(int) * (n+1));  for(i=1; i<=n;i++)         for(j=1; j<=m;j++) {scanf ("%d",&A[i][j]); } maxsum= a[1][1];  for(i=1; i<=m;i++) {memset (b,0, (n+1)*sizeof(int));  for(j=i;j<=m;j++)        {            //Fixed i,j column             for(t=1; t<=n;t++) B[t]+ = A[t][j];//bottom-up (fixed column m,m,n (fixed row n,n,m))s =Sub_sum (b); if(S >maxsum) Maxsum=s; }} printf ("%d\n", maxsum); return 0;}

Maximum sub-array of previous test questions

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.