Hdu1081:to the max (maximum sub-matrix, linear DP)

Source: Internet
Author: User

Title: http://acm.hdu.edu.cn/showproblem.php?pid=1081

Oneself really enough rubbish, obviously did one dimension of this problem, but encountered two-dimensional this topic, unexpectedly will not, I also served (PS: pig AH).

Finally, I looked at the puzzle.

The code is as follows:

#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#defineINF 0x3f3f3f3fusing namespacestd;intn,w[ the][ the],t;intMain () { while(SCANF ("%d", &n)! =EOF) {memset (W,0,sizeof(w));  for(intI=1; i<=n;i++)        {             for(intj=1; j<=n;j++) {scanf ("%d",&t); W[I][J]+=w[i][j-1]+T; }        }        intmaxx=-inf;  for(intI=1; i<=n;i++)        {             for(intj=i;j<=n;j++)            {                intsum=0;  for(intk=1; k<=n;k++)                {                    if(sum<0) sum=0; Sum+=w[k][j]-w[k][i-1]; if(sum>Maxx) Maxx=sum; }}} printf ("%d\n", Maxx); }    return 0;}

The main puzzle of the Great God:

The problem is that the largest field of one dimension is extended to two dimensions, which is done in the process of finding the largest field in one dimension:

intMax_sum (intN) {    intI, j, sum =0, max =-10000;  for(i =1; I <= N; i++)    {        if(Sum <0) Sum=0; Sum+=A[i]; if(Sum >max) Max=sum; }    returnsum;}
Extended to two-dimensional time is the same method, but need to be two-dimensional compression into one dimension, so we have to do the data processing, so that map[i][j] from the expression of the first line of the J element to represent the first J elements and, so map[k][j]-map[k][i] It is possible to represent the elements of the K row from the I->j column and. As long as the Villivido two-layer loop enumeration I and J is ok.
#include <iostream>#defineMAX 101using namespacestd;intMap[max][max];intMain () {intN, I, J, temp, K;  while(SCANF ("%d", &n)! =EOF) {memset (map,0,sizeof(map));  for(i =1; I <= N; i++)             for(j =1; J <= N; J + +) {scanf ("%d", &temp); MAP[I][J]+ = Map[i][j-1] + temp;//This represents the former J list of line I and            }        intmax =-100000;  for(i =1; I <= N; i++)             for(j = i; J <= N; j + +)            {                intsum =0;  for(k =1; K <= N; k++)                {                    if(Sum <0) Sum=0; Sum+ = Map[k][j]-map[k][i-1];//this represents the former K-line, i->j list and                    if(Sum >max) Max=sum; }} printf ("%d\n", Max); }return 0;}

Hdu1081:to the max (maximum sub-matrix, linear DP)

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.