HDU 1081 to the Max

Source: Internet
Author: User

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

The main idea of a matrix is to ask for the maximum of a sub-matrix. The and representations of the sub-matrices are the and of all elements within the matrix.

Analysis:

The map[i][j] array is stored on the first row of the J column and

On line k, dp[k][i] = max (Dp[k][i-1]+map[k][i],map[k][i]), but this only knows

The maximum and the number of the first I of the K line, without knowing which is the most from the first column to the first column on page K.

Daiwa The state transfer equation is converted to, at the end of the K-Act, the most Yamato from column I to column J

DP[I][J] = max (map[k][j]-map[k][i-1]+ dp[i][j] , map[k][j]-map[k][i-1]);

Then use a max to find out the biggest dp[i][j]. Because it does not involve the case of a specific sub-matrix, simply

To get the optimal solution, the state transfer equation can also be written

ans = max ( map[k][j]-map[k][i-1]+  ans ,map[k][j]-map[k][i-1]  )

Ans is the first k line, to the end of the K-Act, the largest and the first to the first column J

Code Listing 1:

#include <stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>#include<algorithm>#defineINF 0x3f3f3f3f#defineN 110using namespacestd;intMaps[n][n], dp[n][n];intMain () {intN, I, J, K, A;  while(~SCANF ("%d", &N)) { for(i =1; I <= N; i++)        {             for(j =1; J <= N; J + +) {scanf ("%d", &a); MAPS[I][J]= Maps[i][j-1] +A; }        }//Maps[i][j] The and of the first J column of line I        intMax =-INF;  for(j =1; J <= N; J + +)        {             for(i =1; I <= J; i++) {Dp[i][j]=0;  for(k =1; K <= N; k++) {Dp[i][j]= Max (Maps[k][j]-maps[k][i-1] + dp[i][j], Maps[k][j]-maps[k][i-1]); if(Dp[i][j] >Max) Max=Dp[i][j]; }}} printf ("%d\n", Max); }    return 0;}
View Code

Code Listing 2:

For details, please refer to:

http://blog.csdn.net/u013611908/article/details/41781431

#include <stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>#include<algorithm>#defineINF 0x3f3f3f3f#defineN 110using namespacestd;intMaps[n][n], dp[n][n];intMain () {intN, I, J, K, A;  while(~SCANF ("%d", &N)) { for(i =1; I <= N; i++) {dp[i][0] =0;  for(j =1; J <= N; J + +) {scanf ("%d", &a); MAPS[I][J]= Maps[i][j-1] +A; }        }//Maps[i][j] The and of the first J column of line I        intMax =-INF, sum;  for(i =1; I <= N; i++)        {             for(j = i; J <= N; j + +) {sum=0;  for(k =1; K <= N; k++)                {                    if(Sum <0) Sum=0; Sum+ = Maps[k][j]-maps[k][i-1]; Max=Max (max, sum); }}} printf ("%d\n", Max); }    return 0;}
View Code

HDU 1081 to the Max

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.