Ultraviolet a 10827-Maximum sum on a torus

Source: Internet
Author: User

Original question:
A grid that wraps both horizontally and vertically is called a torus. given a torus where each cell contains an integer, determine the sub-rectangle with the largest sum. the sum of a sub-rectangle is the sum of all the elements in that rectangle. the grid below shows a torus where the maximum sub-rectangle has been shaded.
 
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 first line in the input contains the number of test cases (at most 18 ). each case 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 100, 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 upgraded version of the previous question (ultraviolet A 108-Maximum Sum. The situation has become much more complicated. This matrix can be rotated cyclically. For example, when all rows are on the rise of a row, the first row will become the last line (originally 2nd rows will become 1st rows, 3rd rows will become 2nd rows ......), When all rows drop by a row, the last row is the first row. Similarly, columns are cyclical. turning all the "rows" into "columns" in the previous sentence is a column loop.
How can this problem be solved?
If you have done any questions related to rings before, you will immediately think of increasing the number of repeated series after the original array. For example, 1, 2, 3, and 3. After processing, it becomes 1, 2, 3, 3, 2, 2, 3. Then, this new sequence can enumerate all consecutive sequences from the ring.
Similarly, this question needs to expand the matrix and double each row of the array storing the matrix numbers and repeat the numbers, each column is also doubled. Finally, a new 2N * 2N large matrix is formed.

In the new large matrix, find the largest sum of the Child matrices whose size is less than or equal to N * N.
A restriction is added: the size of the sub-matrix must be less than or equal to N * N. Therefore, linear scanning is required for the process of "Maximum continuity and, here we need to use the monotonous queue application (previously we had a monotonous queue with a limit on the maximum continuity and length: Max Sum of Max-K-sub-sequence ). A monotonic queue is used to maintain a minimum value with a length less than N.


Code:
[Cpp]
/*
* UVa: 10827-Maximum sum on a torus
* Time: 0.236 s
* Author: D_Double
*/
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
# Include <queue>
# Define maxn250
Using namespace 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];
}
// Conversion
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) {// Enumeration
Que. clear (); // remember to clear it !!
Int prev = 0;
For (int k = 1; k <2 * N; ++ k ){
// Maintain a monotonous queue
While (! Que. empty () & que. back (). val> 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: 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.