Poj #1050-to the max

Source: Internet
Author: User

Looks quite intuitive at the very first sight... I thought:
Rect [x, y, W + 1, H + 1] = rect [x, y, W, H] + num [x + W + 1, y .. Y + H + 1] + num [x .. X + W + 1, Y + H + 1]-num [x + W + 1, Y + H + 1]

But that results in O (N ^ 4) at least.

Reference: http://blog.csdn.net/hitwhylz/article/details/11848439. Lesson learned:Usually 2D issue can be converted to 1D problem to solve. Cool thought. And what is more important:Decoding problem thoroughly and identify familiar pattern in disguise from it.

Here is my AC code:

//    1050//    Ref:    http://blog.csdn.net/hitwhylz/article/details/11848439#include <stdio.h>#define MAX_N 100#define Max(a, b) (a) > (b) ? (a) : (b)int max_sum_1D(int rowSum[MAX_N + 1], int n){    //    dp[i] = max{a[i], dp[i-1] + a[i]}    int ret = -2147483648;    int dp[MAX_N + 1] = { 0 };    for (int k = 1; k <= n; k ++)    {        dp[k] = Max(rowSum[k], dp[k-1] + rowSum[k]);        ret = Max(ret, dp[k]);    }    return ret;}int calc(int in[MAX_N + 1][MAX_N + 1], unsigned n){    int maxSum = -2147483648;    for (int i = 1; i <= n; i++)    {        int rowSum[MAX_N + 1] = { 0 };    // for compact        for (int j = i; j <= n; j++)    // Row[i]..Row[j]        {            //    Compact to 1D array, from row[i] to row[j] for each column            for (int k = 1; k <= n; k++)            {                rowSum[k] += in[j][k];            }            int tmp_sum = max_sum_1D(rowSum, n);            maxSum = Max(tmp_sum, maxSum);        }    }    return maxSum;}int main(){    int n; scanf("%d", &n);    int in[MAX_N + 1][MAX_N + 1];        //    Get input    for (int i = 1; i <= n; i ++)    for (int j = 1; j <= n; j++)    {        scanf("%d", &(in[i][j]));    }    //    int ret = calc(in, n);    printf("%d\n", ret);        return 0;}
View code

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.