Submission #260744

# Submission time Handle Problem Language Result Execution time Memory
260744 2020-08-10T20:13:20 Z idk321 Rectangles (IOI19_rect) Java 11
15 / 100
5000 ms 1048576 KB
import java.util.Arrays;

class rect {
	boolean[][] visited;

	public long count_rectangles(int[][] a) {
		int n = a.length;
		int m = a[0].length;
		int[][] dpUp = new int[n][m];
		int[][] dpDown = new int[n][m];
		int[][] dpLeft = new int[n][m];
		int[][] dpRight = new int[n][m];

		for (int i = 1; i < n - 1; i++) {
			for (int j = m - 2; j > 0; j--) {
				if (a[i - 1][j] > a[i][j]) dpUp[i][j] = dpUp[i][j + 1] + 1;
				if (a[i + 1][j] > a[i][j]) dpDown[i][j] = dpDown[i][j + 1] + 1;
			}
		}
		for (int i = n - 2; i > 0; i--) {
			for (int j = 1; j < m - 1; j++) {
				if (a[i][j + 1] > a[i][j]) dpRight[i][j] = dpRight[i + 1][j] + 1;
				if (a[i][j - 1] > a[i][j]) dpLeft[i][j] = dpLeft[i + 1][j] + 1;
			}
		}

		if (n <= 2 || m <= 2) return 0;

		boolean allZeroAndOne = true;
		for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (a[i][j] != 0 && a[i][j] != 1) allZeroAndOne = false;

		if (n <= 3) {
			int currLength = 0;
			int goodStarts = 0;
			long res = 0;
			for (int i = 1; i < m - 1; i++) {
				int h = a[1][i];
				if (a[0][i] <= h || a[2][i] <= h) {
					goodStarts = 0;
				} else {
					if (a[1][i - 1] > h) goodStarts++;
					if (a[1][i + 1] > h) res += goodStarts;
				}
			}

			return res;
		/*} else if (allZeroAndOne) {
			visited = new boolean[n][m];
			Arrays.fill(visited[0], true);
			Arrays.fill(visited[n - 1], true);
			for (int i = 0; i < n; i++) {
				visited[i][0] = true;
				visited[i][m - 1] = true;
			}
			for (int i = 1; i < n - 1; i++) {
				for (int j = 1; j < m - 1; j++) {

				}
			} */
		} else {
			int[][][] maxRight = new int[n][m][m];
			for (int i = 1; i < n - 1; i++) {
				for (int j = 1; j < m - 1; j++) {
					for (int k = j; k < m - 1; k++) {
						if (j == k) {
							maxRight[i][j][k] = a[i][j];
						} else {
							maxRight[i][j][k] = Math.max(a[i][k], maxRight[i][j][k - 1]);
						}
					}
				}
			}
			int[][][] maxDown = new int[m][n][n];
			for (int j = 1; j < m - 1; j++) {
				for (int i = 1; i < n - 1; i++) {
					for (int k = i; k < n - 1; k++) {
						if (i == k) {
							maxDown[j][i][k] = a[i][j];
						} else {
							maxDown[j][i][k] = Math.max(a[k][j], maxDown[j][i][k - 1]);
						}
					}
				}
			}

			boolean[][][][] okJ = new boolean[n][n][m][m];
			for (int i = 1; i < n - 1; i++) {
				for (int k = i; k < n - 1; k++) {
					for (int j = 1;  j < m - 1; j++) {
						for (int l = j; l < m - 1; l++) {
							okJ[i][k][j][l] = maxRight[k][j][l] < a[k][l + 1] && maxRight[k][j][l] < a[k][j - 1];
							if (k != i) {
								okJ[i][k][j][l] = okJ[i][k][j][l] && okJ[i][k - 1][j][l];
							}
						}
					}
				}
			}
			boolean[][][][] okI = new boolean[n][n][m][m];
			for (int i = 1; i < n - 1; i++) {
				for (int k = i; k < n - 1; k++) {
					for (int j = 1;  j < m - 1; j++) {
						for (int l = j; l < m - 1; l++) {
							okI[i][k][j][l] = maxDown[l][i][k] < a[i - 1][l] && maxDown[l][i][k] < a[k + 1][l];
							if (j != l) {
								okI[i][k][j][l] = okI[i][k][j][l] && okI[i][k][j][l - 1];
							}
						}
					}
				}
			}


			long rect = 0;
			for (int i = 1; i < n - 1; i++) {
				for (int j = 1; j < m - 1; j++) {
					for (int k = i; k < n - 1; k++) {
						if (a[k][j - 1] <= a[k][j]) break;
						for (int l = j; l < m - 1; l++) {
							if (maxDown[l][i][k] >= a[k + 1][l]) break;
							if (maxDown[l][i][k] >= a[i - 1][l]) break;


 							if (okI[i][k][j][l] && okJ[i][k][j][l]) {
								rect++;
								//System.out.println(i + " " + j + " " + k + " " + l);
							}
						}
					}
				}
			}
			//System.out.println(Arrays.deepToString(maxDown));
			//System.out.println(Arrays.deepToString(rightOk));
			//System.out.println(maxDown[1][1][1]);

			return rect;
		}
	}


	private boolean checkIfGood(int[][] ar, int a, int b, int c, int d) {

		for (int i = a; i <= c; i++) {
			int max = 0;
			for (int j = b; j <= d; j++) {
				max = Math.max(max, ar[i][j]);
			}
			if (ar[i][d + 1] <= max) return false;
			if (ar[i][b - 1] <= max) return false;
		}
		for (int j = b; j <= d; j++) {
			int max = 0;
			for (int i = a; i <= c; i++) {
				max = Math.max(max, ar[i][j]);
			}
			if (ar[c + 1][j] <= max) return false;
			if (ar[a - 1][j] <= max) return false;
		}

		return true;
	}
}
# Verdict Execution time Memory Grader output
1 Correct 77 ms 10104 KB Output is correct
2 Correct 120 ms 15056 KB Output is correct
3 Correct 118 ms 14908 KB Output is correct
4 Correct 123 ms 14848 KB Output is correct
5 Correct 117 ms 14944 KB Output is correct
6 Correct 122 ms 14692 KB Output is correct
7 Correct 97 ms 12256 KB Output is correct
8 Correct 88 ms 10616 KB Output is correct
9 Correct 123 ms 14996 KB Output is correct
10 Correct 119 ms 15008 KB Output is correct
11 Correct 117 ms 14844 KB Output is correct
12 Correct 118 ms 14964 KB Output is correct
13 Correct 76 ms 9972 KB Output is correct
14 Correct 74 ms 10216 KB Output is correct
15 Correct 79 ms 10472 KB Output is correct
16 Correct 73 ms 10104 KB Output is correct
17 Correct 74 ms 10088 KB Output is correct
18 Correct 78 ms 10384 KB Output is correct
19 Correct 120 ms 15420 KB Output is correct
20 Correct 100 ms 12648 KB Output is correct
21 Correct 75 ms 10088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 10104 KB Output is correct
2 Correct 120 ms 15056 KB Output is correct
3 Correct 118 ms 14908 KB Output is correct
4 Correct 123 ms 14848 KB Output is correct
5 Correct 117 ms 14944 KB Output is correct
6 Correct 122 ms 14692 KB Output is correct
7 Correct 97 ms 12256 KB Output is correct
8 Correct 88 ms 10616 KB Output is correct
9 Correct 123 ms 14996 KB Output is correct
10 Correct 119 ms 15008 KB Output is correct
11 Correct 117 ms 14844 KB Output is correct
12 Correct 118 ms 14964 KB Output is correct
13 Correct 76 ms 9972 KB Output is correct
14 Correct 74 ms 10216 KB Output is correct
15 Correct 79 ms 10472 KB Output is correct
16 Correct 73 ms 10104 KB Output is correct
17 Correct 1037 ms 127592 KB Output is correct
18 Correct 1349 ms 132408 KB Output is correct
19 Correct 1142 ms 128256 KB Output is correct
20 Correct 1423 ms 132944 KB Output is correct
21 Correct 1039 ms 127548 KB Output is correct
22 Correct 1461 ms 127992 KB Output is correct
23 Correct 1183 ms 130984 KB Output is correct
24 Correct 352 ms 47888 KB Output is correct
25 Correct 74 ms 10088 KB Output is correct
26 Correct 78 ms 10384 KB Output is correct
27 Correct 120 ms 15420 KB Output is correct
28 Correct 100 ms 12648 KB Output is correct
29 Correct 75 ms 10088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 10104 KB Output is correct
2 Correct 120 ms 15056 KB Output is correct
3 Correct 118 ms 14908 KB Output is correct
4 Correct 123 ms 14848 KB Output is correct
5 Correct 117 ms 14944 KB Output is correct
6 Correct 122 ms 14692 KB Output is correct
7 Correct 97 ms 12256 KB Output is correct
8 Correct 88 ms 10616 KB Output is correct
9 Correct 123 ms 14996 KB Output is correct
10 Correct 119 ms 15008 KB Output is correct
11 Correct 117 ms 14844 KB Output is correct
12 Correct 118 ms 14964 KB Output is correct
13 Correct 76 ms 9972 KB Output is correct
14 Correct 74 ms 10216 KB Output is correct
15 Correct 79 ms 10472 KB Output is correct
16 Correct 73 ms 10104 KB Output is correct
17 Correct 1037 ms 127592 KB Output is correct
18 Correct 1349 ms 132408 KB Output is correct
19 Correct 1142 ms 128256 KB Output is correct
20 Correct 1423 ms 132944 KB Output is correct
21 Correct 1039 ms 127548 KB Output is correct
22 Correct 1461 ms 127992 KB Output is correct
23 Correct 1183 ms 130984 KB Output is correct
24 Correct 352 ms 47888 KB Output is correct
25 Execution timed out 5220 ms 1032560 KB Time limit exceeded
26 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 77 ms 10104 KB Output is correct
2 Correct 120 ms 15056 KB Output is correct
3 Correct 118 ms 14908 KB Output is correct
4 Correct 123 ms 14848 KB Output is correct
5 Correct 117 ms 14944 KB Output is correct
6 Correct 122 ms 14692 KB Output is correct
7 Correct 97 ms 12256 KB Output is correct
8 Correct 88 ms 10616 KB Output is correct
9 Correct 123 ms 14996 KB Output is correct
10 Correct 119 ms 15008 KB Output is correct
11 Correct 117 ms 14844 KB Output is correct
12 Correct 118 ms 14964 KB Output is correct
13 Correct 76 ms 9972 KB Output is correct
14 Correct 74 ms 10216 KB Output is correct
15 Correct 79 ms 10472 KB Output is correct
16 Correct 73 ms 10104 KB Output is correct
17 Correct 1037 ms 127592 KB Output is correct
18 Correct 1349 ms 132408 KB Output is correct
19 Correct 1142 ms 128256 KB Output is correct
20 Correct 1423 ms 132944 KB Output is correct
21 Correct 1039 ms 127548 KB Output is correct
22 Correct 1461 ms 127992 KB Output is correct
23 Correct 1183 ms 130984 KB Output is correct
24 Correct 352 ms 47888 KB Output is correct
25 Execution timed out 5220 ms 1032560 KB Time limit exceeded
26 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 92 ms 10744 KB Output is correct
2 Correct 95 ms 10592 KB Output is correct
3 Correct 90 ms 10860 KB Output is correct
4 Correct 76 ms 9868 KB Output is correct
5 Incorrect 87 ms 10604 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 80 ms 10596 KB Output is correct
2 Runtime error 2525 ms 1048576 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 77 ms 10104 KB Output is correct
2 Correct 120 ms 15056 KB Output is correct
3 Correct 118 ms 14908 KB Output is correct
4 Correct 123 ms 14848 KB Output is correct
5 Correct 117 ms 14944 KB Output is correct
6 Correct 122 ms 14692 KB Output is correct
7 Correct 97 ms 12256 KB Output is correct
8 Correct 88 ms 10616 KB Output is correct
9 Correct 123 ms 14996 KB Output is correct
10 Correct 119 ms 15008 KB Output is correct
11 Correct 117 ms 14844 KB Output is correct
12 Correct 118 ms 14964 KB Output is correct
13 Correct 76 ms 9972 KB Output is correct
14 Correct 74 ms 10216 KB Output is correct
15 Correct 79 ms 10472 KB Output is correct
16 Correct 73 ms 10104 KB Output is correct
17 Correct 1037 ms 127592 KB Output is correct
18 Correct 1349 ms 132408 KB Output is correct
19 Correct 1142 ms 128256 KB Output is correct
20 Correct 1423 ms 132944 KB Output is correct
21 Correct 1039 ms 127548 KB Output is correct
22 Correct 1461 ms 127992 KB Output is correct
23 Correct 1183 ms 130984 KB Output is correct
24 Correct 352 ms 47888 KB Output is correct
25 Execution timed out 5220 ms 1032560 KB Time limit exceeded
26 Halted 0 ms 0 KB -