UVa 10827:maximum sum on a torus

Source: Internet
Author: User
Tags empty integer prev first row

Topic Link:

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ problem&problem=1768

Original title:

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

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

Input

The ' the ' the ' the ' the ' the ' the ' number of test cases (at most 18). Each case is starts with an 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 single 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:

15

45

Ideas and Summary:

is the 108-maximum version of the previous question (UVa. Sum). Things are getting a lot more complicated. This matrix can be "rotated". For example, when all rows are up one row, the first line becomes the last line (the 2nd line turns to line 1th, and the 3rd line becomes the 2nd line ...).  When all rows fall in one row, the last line becomes the first row. Similarly, the column is also circular, the last sentence of all the "line" word into the "column" Word is the column loop.

How do you deal with this situation?

If you've ever done something about a ring or something like that, you'll have to think about adding the number of the sequence again after the original array.  such as 1,2,3,4, after processing becomes 1,2,3,4,1,2,3. Then the new sequence can enumerate all contiguous sequences of rings.

Similarly, the question needs to be enlarged to increase the matrix, add one to each row of the array that contains the matrix number, repeat the number, and each column will be doubled. Finally, a large matrix of new 2n*2n is formed.

Then in this new large matrix we find the maximum sum of the n*n of the dimension less than or equal to the matrix.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Due to the addition of a restriction: the size of the child matrix is less than or equal to N*n, then in the process of seeking "maximum continuity and", a linear scan is required, where the application of the monotone queue is used (a question that has previously done a monotone queue for maximum continuity and length limitation: Max Sum of max-k-sub-sequence). The use of a monotonic queue is to maintain a minimum value that is less than n in length.

Code:

* * Uva:10827-maximum sum on a torus * time:0.236s * author:d_double/#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define MAXN-using NA  
      
Mespace std;  struct node{int val;//value int no;  
      
subscript};  
      
int ARR[MAXN][MAXN], SUM[MAXN][MAXN], N, ans;  
    inline void input () {memset (arr, 0, sizeof (arr));  
    memset (sum, 0, sizeof (sum));  
        for (int i=1; i<=n; ++i) {for (int j=1; j<=n; ++j) scanf ("%d", &arr[i][j]);  
    for (int j=n+1; j<2*n; ++j) arr[i][j]=arr[i][j-n];  
    for (int i=n+1; i<2*n; ++i) {for (int j=1; j<2*n; ++j) arr[i][j] = Arr[i-n][j]; }//convert for (int i=1; i<2*n; ++i) {for (int j=1; j<2*n; ++j) sum[i][j] = arr[i][  
    J]+SUM[I][J-1]+SUM[I-1][J]-SUM[I-1][J-1]; }} inline VoiD solve () {deque<node>que;   
    Node temp;  
      
    int maxsum=-2147483646; for (int i=1; i<2*n; ++i) {for (int j= (i-n>=0?i-n:0); j<i; ++j) {//enum que.clear ();/Remember to  
            Empty!!  
            int prev=0; for (int k=1; k<2*n; ++k) {//Maintenance monotonic queue while (!que.empty () && Que.back (). Val &G T  
                prev) que.pop_back ();  
                while (!que.empty () && Que.front (). No < K-n) Que.pop_front ();  
                Temp.val=prev, temp.no=k-1;  
      
                Que.push_back (temp);  
      
                int Val=sum[i][k]-sum[j][k]-que.front (). Val;  
                if (val>maxsum) Maxsum=val;  
            prev = Sum[i][k]-sum[j][k];  
} printf ("%d\n", maxsum);  
    int main () {int T;  
    scanf ("%d", &t);  
        while (t--) {scanf ("%d", &n); Input ();  
        Solve (); }  
}

Author: csdn Blog shuangde800

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.