UVA 10827 Maximum sum on a torus

Source: Internet
Author: User

Original title:
A grid that wraps both horizontally and vertically is called A torus. Given a torus where each cell is contains an integer and determine the Sub-rectangle with the largest sum. The sum of a sub-rectangle is the sum of the "the elements in" that rectangle. The grid below shows
a torus where the maximum sub-rectangle has been shaded.

Input
The first line in the input contains the number of test cases (at most 18). Each case starts with a integer N (1≤n≤75) specifying the size of the torus (always square). Then follows n lines describing the torus, each line containing N integers between-100 and inclusive.
Output
For each test case, Output a line containing a integer:the maximum sum of a sub-rectangle within The torus.
Sample Input
2
5
1-1 0 0-4
2 3-2-3 2
4 1-1 5 0
3-2 1-3 2
-3 2 4 1-4
3
1 2 3
4 5 6
7 8 9
Sample Output

English:
The same lets you find the maximum sub-matrix and, but the four edges can be contiguous across the edge.
Send a set of data
3
4-3 6
-2-4-5
7 0 1

The answer is 18, Four corners.

AC Code

#include <bits/stdc++.h> using namespace std;
int t,n,table[400][400],sum[400];
    int solve () {int ans=int_min;
            for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {memset (sum,0,sizeof (sum));
                for (int k=i+1;k<=n+i;k++) {int tmp=0;
                    for (int s=j+1;s<=n+j;s++) {sum[s]+=table[k][s];
                    Tmp+=sum[s];
                Ans=max (Tmp,ans);
}}}} return ans;
    } int main () {Ios::sync_with_stdio (false);
    cin>>t;
        while (t--) {cin>>n; for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {cin>>table[i][
                J];
            TABLE[I+N][J]=TABLE[I][J+N]=TABLE[I+N][J+N]=TABLE[I][J];
        }} int ans=solve ();
    cout<<ans<<endl;
} return 0;
 }

Tle Code

#include <bits/stdc++.h> using namespace std;
int table[202][202];
int sum[202][202];
int tmp[202];
int n;
    int subseq (int a[]) {int ans=int_min,tmp;
    for (int i=1;i<=n;i++) a[i+n]=a[i];
        for (int k=1;k<n;k++) {tmp= a[k];
        int s = a[k];
            for (int i = k+1; I <= n+k-1; i++) {if (S > 0) s + = A[i];
            else S = a[i];
        TMP = MAX (TMP, s);
    } ans=max (ANS,TMP);
} return ans;
    } int solve () {int max1 = int_min;
    int max2 = Int_max;
        for (int s=n*2;s>n;s--) {memset (sum, 0, sizeof (sum)); for (int i = n, i >= 1; i--) {for (int j = n; j >= 1; j--) sum[i][j] = Sum[i+1][j
        ] + table[s-(n-i)][j];
                } for (int i = n, i >= 1; i--) {for (int j = n+1; j > i; j--) {
               for (int k = 1; k <= N; k++)     TMP[K] = sum[i][k]-sum[j][k];
            ans = max (ans, SUBSEQ (TMP));
}}} return ans;
    } int main () {Ios::sync_with_stdio (false);
    int t;
    cin>>t;
        while (t--) {cin>>n; for (int i = 1, i <= N; i++) {for (int j = 1; J <=n; J + +) {CIN&GT;&G
                T;TABLE[I][J];
            TABLE[I+N][J]=TABLE[I][J];
        }}//cout<<endl;
    Cout<<solve () <<endl;
} return 0;
 }

Ideas:

The largest sub-matrices that can span edges and can be associative to the largest segments of the ring and.
There are two ways to solve the maximal sub-segment of a ring
For example, the requested data is a[]={11,-3,7}
One of the options is to extend a to twice times the length of {11,-3,7,11,-3,7}, and then enumerate the maximum sub-segments with a length of not more than 3 and the time Complexity O (n^2).
Another way is to find the minimum segment of a and then use all of a and subtract the most small segment and is the largest sub-segment of the ring and is very well understood. Time complexity is O (n)

So for this problem, is it possible to draw on the method of solving the ring maximal sub-segment and O (n) algorithm, such as the largest sub-matrix of cross-boundary and, can first find the most small matrix and then the sum of the matrix minus. Unfortunately the method is wrong, as shown in the figure below
The blue circle is the least-boy matrix, and the red circle is the answer to the matrix's largest sub-matrix.

Considering the maximum sub-segment and the method of dividing, can we use this idea to calculate the two-dimensional matrix segmentation. Simply think about it, the first need to expand the Matrix 4 times times, indicating that the pair of edges are adjacent to the situation. Then limit the size of the area to N*n to calculate, the time complexity is probably n^3logn. But it seems complicated, and it's just a thought.

Finally, the most brutal method, directly extending four times times, the enumeration selection area size does not exceed n*n, and then directly calculate and take the maximum value.

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.