diff --git "a/BOJ/1000-5000353円262円210円/JW_1135.java" "b/BOJ/1000-5000353円262円210円/JW_1135.java" new file mode 100644 index 00000000..11b8fc9d --- /dev/null +++ "b/BOJ/1000-5000353円262円210円/JW_1135.java" @@ -0,0 +1,53 @@ +import java.util.ArrayList; +import java.util.Collections; + +public class JW_1135 { + + static int n; + static int[] dp; + static ArrayList> tree = new ArrayList(); + + public static void main(String[] args) throws Exception { + n = read(); + dp = new int[n]; + // 트리 구조 만들기 + for (int i = 0; i < n; i++) { + tree.add(new ArrayList()); + int parent = read(); + // 루트 제외 + if (parent == -1) + continue; + tree.get(parent).add(i); + } + recursive(0); + System.out.println(dp[0]); + } + + // 재귀적으로 해당 노드가 걸리는 계산 + private static void recursive(int cur) { + ArrayList al = new ArrayList(); + for (int next : tree.get(cur)) { + recursive(next); + al.add(dp[next]); // 탐색한 노드가 가지는 값을 저장 + } + // 그리디하게, 가장 오래 걸리는 노드를 먼저 탐색해줘야 함 + al.sort(Collections.reverseOrder()); + + // 1초에 한 번씩 전파 가능 + // 최댓값이 결국 최솟값이 됨 + for (int i = 0; i < al.size(); i++) + dp[cur] = Math.max(dp[cur], al.get(i) + i + 1); + } + + private static int read() throws Exception { + int c, n = System.in.read() & 15; + boolean m = n == 13; + if (m) + n = System.in.read() & 15; + while ((c = System.in.read())>= 48) + n = (n << 3) + (n << 1) + (c & 15); + if (c == 13) + System.in.read(); + return m ? ~n + 1 : n; + } +} diff --git "a/BOJ/1000-5000353円262円210円/JW_1450.java" "b/BOJ/1000-5000353円262円210円/JW_1450.java" new file mode 100644 index 00000000..ccb569ae --- /dev/null +++ "b/BOJ/1000-5000353円262円210円/JW_1450.java" @@ -0,0 +1,73 @@ +import java.util.Map; +import java.util.TreeMap; + +public class JW_1450 { + + static int n, c; + static TreeMap tmA = new TreeMap(); + static TreeMap tmB = new TreeMap(); + static TreeMap prefixSumA = new TreeMap(); + static TreeMap prefixSumB = new TreeMap(); + + public static void main(String[] args) throws Exception { + n = read(); + c = read(); + int[] aArr = new int[n / 2]; + int[] bArr = new int[n - n / 2]; + + for (int i = 0; i < aArr.length; i++) + aArr[i] = read(); + for (int i = 0; i < bArr.length; i++) + bArr[i] = read(); + recursive(0, 0, aArr, tmA); + recursive(0, 0, bArr, tmB); + + makePrefixSum(tmA, prefixSumA); + makePrefixSum(tmB, prefixSumB); + + System.out.println(calculate()); + } + + private static void recursive(int depth, long sum, int[] arr, TreeMap map) { + if (sum> c) + return; + if (depth == arr.length) { + map.put(sum, map.getOrDefault(sum, 0) + 1); + return; + } + recursive(depth + 1, sum + arr[depth], arr, map); + recursive(depth + 1, sum, arr, map); + } + + private static void makePrefixSum(TreeMap tm, TreeMap prefixSum) { + int sum = 0; + for (Map.Entry entry : tm.entrySet()) { + sum += entry.getValue(); + prefixSum.put(entry.getKey(), sum); + } + } + + private static long calculate() { + long count = 0; + + for (Map.Entry entry : tmA.entrySet()) { + long aSum = entry.getKey(); + int aWays = entry.getValue(); + Map.Entry bEntry = prefixSumB.floorEntry(c - aSum); + if (bEntry != null) { + count += (long) aWays * bEntry.getValue(); + } + } + + return count; + } + + private static int read() throws Exception { + int c, n = System.in.read() & 15; + while ((c = System.in.read())>= 48) + n = (n << 3) + (n << 1) + (c & 15); + if (c == 13) + System.in.read(); + return n; + } +} \ No newline at end of file diff --git "a/BOJ/15001-20000353円262円210円/JW_19237.java" "b/BOJ/15001-20000353円262円210円/JW_19237.java" new file mode 100644 index 00000000..e4bc4253 --- /dev/null +++ "b/BOJ/15001-20000353円262円210円/JW_19237.java" @@ -0,0 +1,160 @@ +import java.util.ArrayDeque; +import java.util.Deque; + +public class JW_19237 { + + // 상어 객체 + static class Shark { + int num, idx, dir; // 상어의 번호, 인덱스, 방향 + int[][] priority; // 해당 상어의 위치에 따른 방향 우선 순위 + + Shark(int num, int idx, int dir, int[][] priority) { + this.num = num; + this.idx = idx; + this.dir = dir; + this.priority = priority; + } + + // 방향 우선 순위 반환 + int[] getPriority() { + return priority[dir]; + } + } + + static int n, m, k; + static int[] scentBoard, remainBoard, move; // 냄새, 남은 시간, 방향 벡터 + static boolean[] visited; + static Deque sharks = new ArrayDeque(); // 상어 큐 + static Deque scentDeque = new ArrayDeque(); // 냄새 큐 + + public static void main(String[] args) throws Exception { + n = read(); + m = read(); + k = read(); + move = new int[] { 0, -n, n, -1, 1 }; + scentBoard = new int[n * n]; + remainBoard = new int[n * n]; + visited = new boolean[n * n]; + + // 기본 정보 입력 + init(); + + int time = 0; + while (time <= 1000) { + int size = sharks.size(); + + // 1번 상어만 남았다면 종료 + if (size == 1) { + System.out.println(time); + return; + } + + visited = new boolean[n * n]; // 방문 배열 초기화 + // 상어를 작은 번호 순대로 꺼내서 탐색 + while (size--> 0) { + Shark shark = sharks.poll(); + int num = shark.num, idx = shark.idx; + int nextDir = getNextDir(shark); // 다음 방향 결정 + int next = idx + move[nextDir]; // 이동 + + // 이미 도착해있는 상어가 있다면 + // 즉, 자신보다 작은 번호의 상어가 도착해있다면 쫓겨남 + if (visited[next]) + continue; + + visited[next] = true; + shark.idx = next; + shark.dir = nextDir; + sharks.offer(shark); // 상어 큐에 삽입 + scentDeque.offer(new int[] { next, num }); // 냄새 큐에 삽입 + } + removeScent(); // 냄새 카운트 감소 + putScent(); // 냄새 배열에 적용 + time++; + } + System.out.println(-1); + } + + // 기본 정보 입력 + private static void init() throws Exception { + int[] tempSharks = new int[m + 1]; + for (int i = 0; i < n * n; i++) { + int num = read(); + if (num != 0) { + tempSharks[num] = i; // 상어의 인덱스 저장 + scentBoard[i] = num; // 냄새 삽입 + remainBoard[i] = k; // 남은 시간 기록 + } + } + int[] tempDir = new int[m + 1]; + for (int i = 1; i < m + 1; i++) + tempDir[i] = read(); + + // 상어 객체 생성 및 큐에 삽입 + for (int i = 1; i < m + 1; i++) { + int idx = tempSharks[i], dir = tempDir[i]; + int[][] priority = new int[5][5]; // 우선 순위 입력 + for (int j = 1; j < 5; j++) + for (int k = 1; k < 5; k++) + priority[j][k] = read(); + Shark shark = new Shark(i, idx, dir, priority); + sharks.offer(shark); + } + } + + // 다음 방향 결정 + private static int getNextDir(Shark shark) { + int[] priority = shark.getPriority(); // 우선 순위에 따라 방향 결정 + int myScent = 0; + for (int i = 1; i < 5; i++) { + int dir = priority[i]; + int next = shark.idx + move[dir]; + if (isValid(shark.idx, next)) { + if (scentBoard[next] == 0) + return dir; + else if (scentBoard[next] == shark.num && myScent == 0) + myScent = dir; + } + } + return myScent; + } + + // 냄새 카운트 감소 + private static void removeScent() { + for (int i = 0; i < n * n; i++) + if (scentBoard[i] != 0 && --remainBoard[i] == 0) + scentBoard[i] = 0; + } + + // 냄새 큐에 있는 좌표에 냄새 삽입 + private static void putScent() { + while (!scentDeque.isEmpty()) { + int[] scent = scentDeque.poll(); + int idx = scent[0], num = scent[1]; + scentBoard[idx] = num; + remainBoard[idx] = k; + } + } + + // 인덱스 유효성 검사 + private static boolean isValid(int cur, int next) { + // 전체 인덱스를 벗어나는 경우 + if (next < 0 || next>= n * n) + return false; + // 좌, 우로 움직일 때, 같은 행에 있는지 확인 + int curRow = cur / n; + int nextRow = next / n; + if (Math.abs(cur - next) == 1 && curRow != nextRow) + return false; + return true; + } + + private static int read() throws Exception { + int c, n = System.in.read() & 15; + while ((c = System.in.read())>= 48) + n = (n << 3) + (n << 1) + (c & 15); + if (c == 13) + System.in.read(); + return n; + } +} diff --git "a/CodeTree/2023-2024353円205円204円/JW_352円263円240円353円214円200円_353円254円270円353円252円205円_354円234円240円354円240円201円_355円203円220円354円202円254円.java" "b/CodeTree/2023-2024353円205円204円/JW_352円263円240円353円214円200円_353円254円270円353円252円205円_354円234円240円354円240円201円_355円203円220円354円202円254円.java" new file mode 100644 index 00000000..8353d73e --- /dev/null +++ "b/CodeTree/2023-2024353円205円204円/JW_352円263円240円353円214円200円_353円254円270円353円252円205円_354円234円240円354円240円201円_355円203円220円354円202円254円.java" @@ -0,0 +1,136 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Deque; +import java.util.PriorityQueue; +import java.util.StringTokenizer; + +public class JW_고대_문명_유적_탐사 { + + static int k, m, total = 0; + static int[][] board = new int[5][5], nextBoard = new int[5][5]; // 각종 보드 + static Deque dq = new ArrayDeque(); // 벽면에 적힌 수를 저장할 덱 + static PriorityQueue pq = new PriorityQueue( // 적힌 수를 삽입하기 위한 좌표 + (o1, o2) -> o2[1] != o1[1] ? o1[1] - o2[1] : o2[0] - o1[0]); + + static int[] dy = { -1, 1, 0, 0 }, dx = { 0, 0, -1, 1 }; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + k = Integer.parseInt(st.nextToken()); + m = Integer.parseInt(st.nextToken()); + for (int i = 0; i < 5; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 0; j < 5; j++) + board[i][j] = Integer.parseInt(st.nextToken()); + } + st = new StringTokenizer(br.readLine()); + // 벽면에 적힌 수 + for (int i = 0; i < m; i++) + dq.offer(Integer.parseInt(st.nextToken())); + StringBuilder sb = new StringBuilder(); + while (k--> 0) { + // 회전 -> 열 -> 행 순으로 입력 + for (int t = 0; t < 4; t++) + for (int i = 1; i < 4; i++) + for (int j = 1; j < 4; j++) { + int[][] tempBoard = rotate(j, i, t); + PriorityQueue nPq = earnValue(tempBoard); + // 더 많은 가치를 얻을 수 있다면 + if (nPq.size()> pq.size()) { + pq = nPq; + nextBoard = tempBoard; + } + } + // 종료 조건 + if (pq.size() == 0) + break; + int totalValue = pq.size(); + board = nextBoard; + fillValue(); + // 유물 연쇄 획득 + while (true) { + pq = earnValue(board); + // 더 이상 얻을 수 없다면 종료 + if (pq.isEmpty()) + break; + totalValue += pq.size(); + fillValue(); + } + sb.append(totalValue).append(" "); + } + System.out.println(sb); + } + + // 회전 + private static int[][] rotate(int sy, int sx, int time) { + int[][] tempBoard = new int[5][5]; // 임시 배열 + int[][] nextBoard = new int[5][5]; // 회전 배열 + // 깊은 복사 + for (int i = 0; i < 5; i++) + nextBoard[i] = board[i].clone(); + int y = sy - 1, x = sx - 1; // 초깃값 보정 + // 회전 카운트만큼 회전 + while (time--> 0) { + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + tempBoard[y + j][x + 2 - i] = nextBoard[y + i][x + j]; + + // 임시 배열에서 회전한 값을 원래 배열에 적용 + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + nextBoard[y + i][x + j] = tempBoard[y + i][x + j]; + } + return nextBoard; + } + + // 총 가칠 + private static PriorityQueue earnValue(int[][] nextBoard) { + boolean[][] visited = new boolean[5][5]; + // 열과 행을 기준으로 값이 채워져야하므로 우선순위 큐를 사용 + PriorityQueue nPq = new PriorityQueue((o1, o2) -> o2[1] != o1[1] ? o1[1] - o2[1] : o2[0] - o1[0]); + for (int y = 0; y < 5; y++) + for (int x = 0; x < 5; x++) + bfs(y, x, nextBoard, visited, nPq); // 유물의 조각이 연결되었는지 확인 + return nPq; + } + + // 유물 조각 연결 유무를 판단하기 위한 BFS + private static void bfs(int sy, int sx, int[][] nextBoard, boolean[][] visited, PriorityQueue nPq) { + Deque dq = new ArrayDeque(); + ArrayList al = new ArrayList(); // 사용된 좌표 리스트 + dq.offer(new int[] { sy, sx }); + al.add(new int[] { sy, sx }); + visited[sy][sx] = true; + while (!dq.isEmpty()) { + int[] cur = dq.poll(); + int y = cur[0], x = cur[1]; + for (int i = 0; i < 4; i++) { + int ny = y + dy[i], nx = x + dx[i]; + if (isValid(ny, nx) && !visited[ny][nx] && nextBoard[ny][nx] == board[y][x]) { + dq.offer(new int[] { ny, nx }); + al.add(new int[] { ny, nx }); + visited[ny][nx] = true; + } + } + } + if (al.size()> 2) { + for (int[] pos : al) + nPq.offer(pos); + } + } + + private static void fillValue() { + while (!pq.isEmpty()) { + int[] cur = pq.poll(); + int y = cur[0], x = cur[1], v = dq.poll(); + board[y][x] = v; + } + } + + private static boolean isValid(int y, int x) { + return 0 <= y && y < 5 && 0 <= x && x < 5; + } +} diff --git a/Programmers/Level2/JW_389479.java b/Programmers/Level2/JW_389479.java new file mode 100644 index 00000000..59ce6c82 --- /dev/null +++ b/Programmers/Level2/JW_389479.java @@ -0,0 +1,16 @@ +class JW_389479 { + public int solution(int[] players, int m, int k) { + int n = players.length; + int total = 0; + for (int i = 0; i < n; i++) { + int needs = (int) Math.floor((double) players[i] / m); // 증설해야하는 서버 수 + if (needs == 0) + continue; + total += needs; // 증설 횟수 누적 + // k시간 동안 유지 가능 + for (int j = 0; j < k && i + j < n; j++) + players[i + j] = Math.max(0, players[i + j] - m * needs); + } + return total; + } +} diff --git "a/SQL/23354円243円274円354円260円250円/354円241円260円352円261円264円354円227円220円_353円247円236円353円212円224円_352円260円234円353円260円234円354円236円220円_354円260円276円352円270円260円.sql" "b/SQL/23354円243円274円354円260円250円/354円241円260円352円261円264円354円227円220円_353円247円236円353円212円224円_352円260円234円353円260円234円354円236円220円_354円260円276円352円270円260円.sql" new file mode 100644 index 00000000..73a8505d --- /dev/null +++ "b/SQL/23354円243円274円354円260円250円/354円241円260円352円261円264円354円227円220円_353円247円236円353円212円224円_352円260円234円353円260円234円354円236円220円_354円260円276円352円270円260円.sql" @@ -0,0 +1,12 @@ +SELECT + ID, + EMAIL, + FIRST_NAME, + LAST_NAME +FROM + DEVELOPERS +WHERE + (SKILL_CODE & (SELECT CODE FROM SKILLCODES WHERE NAME = 'Python')) + OR (SKILL_CODE & (SELECT CODE FROM SKILLCODES WHERE NAME = 'C#')) +ORDER BY + ID; \ No newline at end of file

AltStyle によって変換されたページ (->オリジナル) /