patternjavaMinor
Orchard Partitioning in to close-to-equal sectors
Viewed 0 times
closesectorsequalorchardpartitioning
Problem
The task (from this site) is to create a program that will solve a problem where a matrix (size n * n), which contains integers, is to be divided into four parts, the sums of which are as close to each other as possible. They have to be divided left to right all the way and those parts (top/bottom), can be divided without the imaginary line touching. The output is the difference between the part with the largerst sum and the one with the smallest.
```
public class Main {
Scanner scan = new Scanner(System.in);
int [][] area_values;
int size;
int max_co;
int y_divide;
int x_divide_top;
int x_divide_bottom;
int left_top;
int right_top;
int left_bottom;
int right_bottom;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Main main = new Main();
main.read();
main.division();
main.result();
}
void read() {
size = scan.nextInt();
arrayLoad();
max_co = size - 1;
}
void arrayLoad() {
area_values = new int [size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
add(scan.nextInt(),i,j);
}
}
}
void add(int num, int x, int y) {
for (int i = x; i < size; i++) {
for (int j = y; j < size; j++) {
area_values[i][j] += num;
}
}
}
int block_value(int by, int bx, int ey, int ex) {
int value = area_values[ex][ey];
if (bx == 0 && by ==0) {}
else if (bx != 0 && by !=0) {
value -= area_values[bx-1][ey];
value -= area_values[ex][by-1];
value += area_values[bx-1][by-1];
}
else if (bx == 0) {
value -= area_values[ex][by-1];
}
else if (by == 0) {
value -= area_values[bx-1][ey];
}
return value;
}
void division() {
east
```
public class Main {
Scanner scan = new Scanner(System.in);
int [][] area_values;
int size;
int max_co;
int y_divide;
int x_divide_top;
int x_divide_bottom;
int left_top;
int right_top;
int left_bottom;
int right_bottom;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Main main = new Main();
main.read();
main.division();
main.result();
}
void read() {
size = scan.nextInt();
arrayLoad();
max_co = size - 1;
}
void arrayLoad() {
area_values = new int [size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
add(scan.nextInt(),i,j);
}
}
}
void add(int num, int x, int y) {
for (int i = x; i < size; i++) {
for (int j = y; j < size; j++) {
area_values[i][j] += num;
}
}
}
int block_value(int by, int bx, int ey, int ex) {
int value = area_values[ex][ey];
if (bx == 0 && by ==0) {}
else if (bx != 0 && by !=0) {
value -= area_values[bx-1][ey];
value -= area_values[ex][by-1];
value += area_values[bx-1][by-1];
}
else if (bx == 0) {
value -= area_values[ex][by-1];
}
else if (by == 0) {
value -= area_values[bx-1][ey];
}
return value;
}
void division() {
east
Solution
You definitely should use a
Besides, your reading subroutine works in O(n4), which, for n about 103, is a bit too much. I think your program just gets killed after it exceeds the time limit, leaving you thinking that you are very close.
You seem to want to construct a matrix of patrial sums. Can you think of a way to construct in it O(n2)?
Also, your solution is not correct. Consider a test
Your solution outputs 4, when the correct answer is obviously 2.
BufferedReader. Take a look at the class I use when I am solving problems of this kind.Besides, your reading subroutine works in O(n4), which, for n about 103, is a bit too much. I think your program just gets killed after it exceeds the time limit, leaving you thinking that you are very close.
You seem to want to construct a matrix of patrial sums. Can you think of a way to construct in it O(n2)?
Also, your solution is not correct. Consider a test
4
0 4 0 0
2 0 0 0
2 0 0 0
0 2 0 0Your solution outputs 4, when the correct answer is obviously 2.
Code Snippets
4
0 4 0 0
2 0 0 0
2 0 0 0
0 2 0 0Context
StackExchange Code Review Q#42900, answer score: 2
Revisions (0)
No revisions yet.