Saturday, August 28, 2010

poj_1050_ToTheMax.cpp

Problem Links:

poj1050, uva00108,

Problem:

To the Max
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 22580 Accepted: 11759
Description
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*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 * 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
Source
Greater New York 2001

Solution:

Dynamic programming.

Source Code:


//Thu Mar 11 12:12:25 CST 2010
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int MaxSum(int n, int a[])
{
    int i, max = -12800, sum = 0;
    for (i = 0; i < n; i++)
    {
        if (sum > 0)
            sum += a[i];
        else
            sum = a[i];
        if (max < sum)
            max = sum;
    }
    return max;
}
int MaxSum2(int n, int a[][100])
{
    int max = -12800;
    int b[100];
    int i, j, k;
    for (i = 0; i < n; i++) //Start from row# i.
    {
        for (j = 0; j < n; j++)
            b[j] = 0; //The sum of elements from row i to row k. j control the column.
        for (k = i; k < n; k++)
        {
            for (j = 0; j < n; j++)
                b[j] += a[k][j];
            int sum = MaxSum(n, b);
            if (max < sum)
                max = sum;
        }
    }
    return max;
}
int main(int argc, char* argv[])
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n;
    int a[100][100];
    while (cin >> n)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                cin >> a[i][j];
            }
        }
        cout << MaxSum2(n, a) << endl;
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}

No comments :