제출 #261178

#제출 시각아이디문제언어결과실행 시간메모리
261178idk321Rectangles (IOI19_rect)Java
50 / 100
3479 ms1048584 KiB
import java.util.Arrays;

class rect {
	boolean[][] visited;

	public long count_rectangles(int[][] a) {
		int n = a.length;
		int m = a[0].length;

		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 == -1) {
			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 if (allZeroAndOne) {
			int[][][] dpRight = new int[2][n][m];
			int[][][] dpDown = new int[2][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]) {
						if (dpRight[1][i][j + 1] != 0)dpRight[1][i][j] = dpRight[1][i][j + 1] + 1;
						else if (a[i][j + 1] > a[i][j]) dpRight[1][i][j] = 1;
					}
					if (a[i - 1][j] > a[i][j]) {
						if (dpRight[0][i][j + 1] != 0)dpRight[0][i][j] = dpRight[0][i][j + 1] + 1;
						else if (a[i][j + 1] > a[i][j]) dpRight[0][i][j] = 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]) {
						if (dpDown[0][i  +1][j] != 0) dpDown[0][i][j] = dpDown[0][i + 1][j] + 1;
						else if (a[i + 1][j] > a[i][j]) dpDown[0][i][j] = 1;
					}
					if (a[i][j + 1] > a[i][j]) {
						if (dpDown[1][i  +1][j] != 0) dpDown[1][i][j] = dpDown[1][i + 1][j] + 1;
						else if (a[i + 1][j] > a[i][j]) dpDown[1][i][j] = 1;
					}
				}
			}

			long rect = 0;
			for (int i = 1; i < n - 1; i++) {
				for (int j = 1; j < m - 1; j++) {
					int x = dpRight[0][i][j];
					int y = dpDown[0][i][j];
					if (x == 0) continue;
					if (y == 0) continue;
					//System.out.println(i + " " + j + " " + x + " " + y);
					if (dpRight[1][i + y - 1][j] == x && dpDown[1][i][j + x - 1] == y) {
						rect++;
					}
				}
			}
/*
4 5
1 1 0 1 1
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1
*/
			return rect;
		} 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]);
						}
					}
				}
			}



			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++) {
						for (int l = j; l < m - 1; l++) {
							if (maxDown[l][i][k] >= a[i - 1][l] || maxDown[l][i][k] >= a[k + 1][l]) break;

							boolean shouldBreak = false;
							boolean possible = true;
							for (int o = i; o <= k; o++) {
								if (maxRight[o][j][l] >= a[o][j - 1]) {
									shouldBreak = true;
									break;
								}
								if (maxRight[o][j][l] >= a[o][l + 1]) {
									possible = false;
									break;
								}
							}
							if (shouldBreak) break;

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

			return rect;
		}
	}

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...