ZOJ 1074 To the Max (DP)

Problem

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.

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.

Example

Input

4

0 -2 -7 0 9 2 -6 2

-4 1 -4 1 -1

8 0 -2

Output

15

水,直接水过。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
using namespace std;
int a[110][110];
int n,x;

int main()
{
    int sum,maxn;
    while(~scanf("%d",&n))
    {
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&x);
                a[i][j]=a[i-1][j]+x;
            }
        }
        maxn=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=i;j<=n;j++)
            {
                sum=0;
                for(int k=1;k<=n;k++)
                {
                    int t=a[j][k]-a[i-1][k];
                    sum+=t;
//                    cout<<"fuck "<<sum<<endl;
                    if(sum<0)
                        sum=0;
                    if(sum>maxn)
                        maxn=sum;
                }
            }
        }
        printf("%d\n",maxn);
    }
    return 0;
}

ZOJ 1074 To the Max (DP)

时间: 2024-12-19 17:04:39

ZOJ 1074 To the Max (DP)的相关文章

ZOJ 1642 Match for Bonus(dp)

Match for Bonus Time Limit: 2 Seconds      Memory Limit: 65536 KB Roy played a game with his roommates the other day. His roommates wrote 2 strings of characters, and gave each character a bonus value. When Roy pinned the positions of one common char

ZOJ 3631 Watashi&#39;s BG(dp+dfs)

题意:一共要吃n顿饭 公款m元 如果公款大于等于饭局所需费用 就全用公款 如果小于就自费 求最后能用的公款为多少 思路: dfs(i - 1, val + dp[i]); dfs(i - 1, val); #include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; int dp[50]; int n, m; int a

ZOJ 3644 Kitty&#39;s Game(DP)

Description Kitty is a little cat. She is crazy about a game recently. There are n scenes in the game(mark from 1 to n). Each scene has a number pi. Kitty's score will become least_common_multiple(x,pi) when Kitty enter the ith scene. x is the score

ZOJ 1743 Concert Hall Scheduling(DP)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=743 题意:有两个音乐厅出租.给出n个租客,每个租客有个租的时间段[L,R],以及租费.任意时候音乐厅只能租给最多一个租客.问如何选择租给哪些租客使得赚的钱最多? 思路:f[i][j]表示第一个音乐厅到时刻i.第二个到时刻j,可以获得的最大值. struct node { int x,y,w; int operator<(const node &a) const

zoj 3706 Break Standard Weight(dp)

Break Standard Weight Time Limit: 2 Seconds                                     Memory Limit: 65536 KB The balance was the first mass measuring instrument invented. In its traditional form, it consists of a pivoted horizontal lever of equal length ar

poj - 1050 - To the Max(dp)

题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 -->>将二维压缩为一维,对一维进行dp求解. 将二维压缩成一维: 1.第1行 2.第2行加第1行 3.第3行加第2行加第1行 -- N.第N行加第N-1行加--加第1行 1.第2行 2.第3行加第2行 -- 1.第N行 对于一维情况,设dp[i]表示以第i个元素结尾的最大连续和,则状态转移方程为

POJ 1050 To the Max(DP,最大子矩阵和)

POJ 1050 题意:给一个矩阵,求出元素和最大的子矩阵. 思路: 之前曾写过最大子串和的一篇文章,这次由一维上升到了二维. 我们可以通过累加每行相同列或每列相同行的值,将其储存在一个数组中,便可以将二维降至一维. 时间复杂度为O(n^3). 参考: 累加每一行相同列的做法(java实现) 累加每一列相同行的做法(C++实现) code: /* *Author : Flint_x *Created Time : 2015-07-23 15:10:01 *File name : POJ1050.

hdu 1081 To The Max(dp+化二维为一维)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1081 To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8839    Accepted Submission(s): 4281 Problem Description Given a two-dimensional ar

ZOJ 1642 Match for Bonus (DP)

题目链接 题意 : 给你两个字符串,两个字符串都有共同的字母,给你每个字母的值,规则是,找出两个字符串中的共同的一个字母,然后这个字母的值就可以加到自己的分数上,但是这步操作之后,这两个字母及其之前的字母都要删除掉,问你能够得到的最大的值是什么. 思路:最长公共子序列,加了一个权值. 1 #include <iostream> 2 #include <stdio.h> 3 #include <string> 4 #include <string.h> 5 6