HDOJ 1081(ZOJ 1074) To The Max(动态规划)

Problem Description
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.

As an example, the maximal sub-rectangle of the array:

0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2

is in the lower left corner:

9 2
-4 1
-1 8

and has a sum of 15.

Input
The input consists of an N x N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N 2 integers separated by whitespace (spaces and newlines). These are the N 2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].

Output
Output the sum of the maximal sub-rectangle.

Sample Input
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1
8 0 -2

Sample Output
15

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int a[2000];
int dp[150][150];

int main(){
   int n;
   while(scanf("%d",&n)==1){
        int t;
      memset(dp,0,sizeof(dp));
      for(int i=1;i<=n;i++){
         for(int j=1;j<=n;j++){
            scanf("%d",&t);
            dp[i][j]=t+dp[i-1][j];
           /// printf("i=%d",i);
         }
      }
//        for(int i=0;i<=n;i++){
//         for(int j=0;j<=n;j++){
//                printf("%4d",dp[i][j]);
//         }
//         printf("\n");
//        }
      int maxx=-1000;
      for(int i=1;i<=n;i++){
          for(int j=i;j<=n;j++){
                int sum=0;
              for(int k=1;k<=n;k++){
                    t=dp[j][k]-dp[i-1][k];
                    sum+=t;
                    if(sum<0)  sum=0;
                    if(sum>maxx) maxx=sum;
              }
          }
      }
      printf("%d\n",maxx);
   }
   return 0;
}
时间: 2024-09-17 20:03:13

HDOJ 1081(ZOJ 1074) To The Max(动态规划)的相关文章

HDOJ/HDU 2710 Max Factor(素数快速筛选~)

Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) cows with a distinct serial number in the range 1..20,000. Unfortunately, he is unaware that the cows interpret some serial numbers as be

HDOJ 2071 Max Num

Problem Description There are some students in a class, Can you help teacher find the highest student . Input There are some cases. The first line contains an integer t, indicate the cases; Each case have an integer n ( 1 ≤ n ≤ 100 ) , followed n stu

HDOJ 1003 Max Sum

Problem Description Given a sequence a[1],a[2],a[3]--a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input contains an

ZOJ和PKU 题目分类

ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 1334 1337 1338 1350 1365 1382 1383 1394 1402 1405 1414 1494 1514 1622 1715 1730 1755 1760 1763 1796 1813 1879 1889 1904 1915 1949 2001 2022 2099 2104 21

【DP专辑】ACM动态规划总结

转载请注明出处,谢谢.   http://blog.csdn.net/cc_again?viewmode=list          ----------  Accagain  2014年5月15日 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强,主要考察思维能力.建模抽象能力.灵活度. 本人动态规划博客地址:http://blog.csdn.net/cc_again/article/category/1261899 ******************

经典动态规划基础题-三角形最大和问题

三角形最大和问题 Time Limit:1000MS Memory Limit:65536K Total Submit:79 Accepted:22 Description 现在经常有一些数学问题困扰着小明.有如下一个三角形, 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 小明想求出从顶至底的某处的一条路径,使该路径所经过的数字的总和最大.现在想请你编一个程序实现这个问题. 说明: (1)每一步可沿左斜线向下或右斜线向下: (2)1<三角形行数≤100; (3)三角形中的数字为0,

动态规划(DP)入门

零.先修课程 首先,在开始理解DP的思想前,你需要 1. 完成HDU里面的递推求解专题练习(For Beginner)那7道题(这些题很简单,题解请在博客中搜索),这对你理解DP有很大的帮助. 2. 对递归搜索(比如深度优先搜索,DFS)有一定了解. 一.递归与记忆化搜索 我们从POJ 3176入手来学习这一思想.(题目很短,请快速读完) 从上往下看,最大和值无非是往左走和往右走这两条路的较大者.这样,我们可以写出下面这个重要的递归函数:(这里i表示行,j表示列) int f(int i, in

算法:zoj 3201 Tree of Tree(树形背包dp)

题意 给一棵节点带权的树,找到一个有k个节点的子树,求这个子树的最大权值 思路 树形 dp+背包. f(i, j) 表示以i为根节点的有j个节点子树的最大权值 然后对i的每个子节点做分组背包, 因为对于i的每个儿子,可以选择分配 1,2,3...j-1个节点给它 f(i, j) = max{ max{f(i, j-p) + f(v, p) | 1<=p<j} | v是i的儿子节点} ans = max{ f[i][k] | 0<=i<n && i 子树节点个数>

算法:ZOJ 3659 Conquer a New Region(并查集)

[题目大意] 有N个城市,N-1条路把这些城市连起来(刚好是一个树).相邻的两个城市有一个 运输容量C(i, j),而城市x到城市y的那条路的运输能力S取决与这条路上所经过的所有路中最小的那个容量 . 以那一个城市为中心,到其他N-1个城市的运输能力总和最大? [思路] 用神奇的并查集 ,把路按照权值从大到小排序,然后用类似Kruskal的方法不断的加入边. 对于要加入的一条路,这条路连 接这城市x和y,x所在的集合为A, y所在的集合为B, 可以确定A,B集合内的所有路都比当前这条路的权值 大