Created
March 23, 2023 14:35
-
-
Save sedin2/aaf73aa07116e427c535733aea919d6e to your computer and use it in GitHub Desktop.
코드트리_정수 사각형 최장 증가 수열
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
public class Main { | |
static int n; | |
static int[][] grid; | |
static int[][] dp; | |
static int[] dx = {-1, 1, 0, 0}; | |
static int[] dy = {0, 0, -1, 1}; | |
static int answer; | |
public static void main(String[] args) throws Exception { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); | |
StringBuilder sb = new StringBuilder(); | |
StringTokenizer st; | |
n = Integer.parseInt(br.readLine()); | |
grid = new int[n][n]; | |
dp = new int[n][n]; | |
PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> Integer.compare(o1[2], o2[2])); | |
for (int i=0; i<n; i++) { | |
st = new StringTokenizer(br.readLine(), " "); | |
Arrays.fill(dp[i], 1); | |
for (int j=0; j<n; j++) { | |
grid[i][j] = Integer.parseInt(st.nextToken()); | |
pq.offer(new int[]{i, j, grid[i][j]}); | |
} | |
} | |
while (!pq.isEmpty()) { | |
int[] cur = pq.poll(); | |
int x = cur[0]; | |
int y = cur[1]; | |
int num = cur[2]; | |
for (int i=0; i<4; i++) { | |
int nx = x + dx[i]; | |
int ny = y + dy[i]; | |
if (nx < 0 || nx >= n || ny < 0 || ny >= n) { | |
continue; | |
} | |
if (num < grid[nx][ny]) { | |
dp[nx][ny] = Math.max(dp[nx][ny], dp[x][y] + 1); | |
} | |
} | |
answer = Math.max(answer, dp[x][y]); | |
} | |
sb.append(answer); | |
bw.write(sb.toString()); | |
bw.close(); | |
br.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment