2013
11-09

# To the Max

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.

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 the sum of the maximal sub-rectangle.

4
0 -2 -7 0 9 2 -6 2
-4 1 -4  1 -1

8  0 -2

15

import java.util.Scanner;

public class Main {

int[][] a;
int l;
int max = Integer.MIN_VALUE;
int t;
int t1;
int t2[];

public Main() {
Scanner scan = new Scanner(System.in);
l = scan.nextInt();
a = new int[l][l];
for (int i = 0; i < l; i++) {
for (int j = 0; j < l; j++) {
a[i][j] = scan.nextInt();
}
}
search();
System.out.println(max);
}

public void search() {
for (int i = 0; i < l; i++) {
for (int j = 0; j < l; j++) {
t1 = 0;
t2 = new int[l];
for (int k = i; k < l; k++) {
t1 += a[k][j];
t = t1;
if (t > max) {
max = t;
}
for (int m = j + 1; m < l; m++) {
t2[m] += a[k][m];
t += t2[m];
if (t > max) {
max = t;
}
}
}
}
}
}

public static void main(String[] args) {
new Main();
}
}

1. 换句话说，A[k/2-1]不可能大于两数组合并之后的第k小值，所以我们可以将其抛弃。
应该是，不可能小于合并后的第K小值吧

2. Often We don’t set up on weblogs, but I would like to condition that this established up really forced me individually to do this! considerably outstanding publish

3. 第一句可以忽略不计了吧。从第二句开始分析，说明这个花色下的所有牌都会在其它里面出现，那么还剩下♠️和♦️。第三句，可以排除2和7，因为在两种花色里有。现在是第四句，因为♠️还剩下多个，只有是♦️B才能知道答案。