diff --git a/week17/Baekjoon1516.java b/week17/Baekjoon1516.java index e078aef..fef929a 100644 --- a/week17/Baekjoon1516.java +++ b/week17/Baekjoon1516.java @@ -1,3 +1,53 @@ +import java.util.*; +import java.io.*; + +/** + * 25/06/16 + * Java8 | 실행시간: 160 ms, 메모리 : 17,424KB + */ public class Baekjoon1516 { + + public static void main(String[] args) throws IOException { + int N = readInt(); + + int[] inDegree = new int[N+1]; + int[] cost = new int[N+1]; + int[] result = new int[N+1]; + ArrayList> arr = new ArrayList<>(); + for (int i=0;i<=N;i++) arr.add(new ArrayList<>()); + Queue q = new ArrayDeque<>(); + + for (int v=1;v<=N;v++){ + cost[v] = readInt(); + int u = 0; + while((u=readInt())!=-1){ + arr.get(u).add(v); + inDegree[v]++; + } + } + + for (int i=1;i<=N;i++) { + if (inDegree[i]==0){ + q.offer(i); + result[i] = cost[i]; + } + } + + while(!q.isEmpty()){ + int c = q.poll(); + for (int nextPoint : arr.get(c)){ + result[nextPoint] = Math.max(result[nextPoint], result[c]+cost[nextPoint]); + if (--inDegree[nextPoint]==0) q.offer(nextPoint); + } + } + StringBuilder sb = new StringBuilder(); + for (int i=1;i<=N;i++) sb.append(result[i]).append("\n"); + System.out.print(sb); + } + private static final StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); + private static int readInt() throws IOException{ + st.nextToken(); + return (int) st.nval; + } } diff --git a/week17/Baekjoon17182.java b/week17/Baekjoon17182.java index 8b41251..94213a4 100644 --- a/week17/Baekjoon17182.java +++ b/week17/Baekjoon17182.java @@ -1,3 +1,48 @@ +import java.io.*; +/** + * 250616 + * Java8 | 실행시간: 96 ms, 메모리: 11,912KB + */ public class Baekjoon17182 { + static int N, dt[][], result = Integer.MAX_VALUE; + static boolean[] isVisited; + + public static void main(String[] args) throws IOException { + // 모든 행성을 탐사하기 위한 최소 시간 출력 + // 다시 시작 행성으로 돌아올 필요 X, 중복 방문 가능 + N = readInt(); + int K = readInt(); // 0<=K=result) return result; // 가지치기 + if (ct==N-1) return sum; + + for (int i=0;i{ + int P, L; + Node(int P, int L){ + this.P = P; // 문제번호 + this.L = L; // 문제난이도 + } + + @Override + public int compareTo(Node o) { + return (this.L!=o.L) ? Integer.compare(this.L, o.L) : Integer.compare(this.P, o.P); + } + } + private static TreeSet list = new TreeSet(); + private static HashMap P2L = new HashMap<>(); // 문제번호 -> 난이도 조회 + + public static void main(String[] args) throws IOException { + int N = readInt(); // 문제 개수 + for (int i=0;i minTree, maxTree 로 분리 +// 분리했을 때 메모리(32,552 KB) / 이전 구조 메모리(36,716 KB) + +// 추가적으로 세그먼트 트리 크기 구할 때, while 문으로 start 인덱스 구하는 거 말고 (2^start>=N) 식 활용해서 수식으로 구하는 것도 해봤는데 별 효과 없어서 제거함 +// 결국 while 로 구해도 O(logN) 이라서 크게 효과 없는 듯 + +/** + * 250617듯 + * Java8 | 실행시간: 372 ms, 메모리: 32552 KB + */ +import java.io.*; + public class Baekjoon2357 { + + private static class SegTree{ + int size, start; + int[] minTree; + int[] maxTree; + + SegTree(int n){ + size = n; + start = 1; + + while(start < size) start <<= 1; + minTree = new int[start*2]; + maxTree = new int[start*2]; + } + + // 선입력 받은 리프 노드로 전체 세그먼트 트리 구성 + // 하나의 공간은 그 구간의 최솟값, 최댓값을 가지고 있음 + void construct(){ + for (int i=start-1; i>0; i--) { + minTree[i] = Math.min(minTree[i*2], minTree[i*2+1]); + maxTree[i] = Math.max(maxTree[i*2], maxTree[i*2+1]); + } + } + + // [a, b] 구간의 최댓값, 최솟값 조회 + Node getMinMax(int a, int b){ + a += start; + b += start; + int min = Integer.MAX_VALUE; + int max = Integer.MIN_VALUE; + + while (a <= b){ // a 와 b 가 겹칠 때까지 왼쪽/오른쪽에서 가운데로 좁혀오도록 이동 (최고 높이의 공통 부모 노드까지 이동) + // 구간을 쪼갰을 때 양쪽 경계에 걸쳐 있는 예외 노드를 따로 챙겨주자 + if ((a&1)==1){ // (1) a가 부모 노드의 오른쪽 노드일 때 + min = Math.min(min, minTree[a]); + max = Math.max(max, maxTree[a]); + a++; + } + if ((b&1)==0){ // (2) b가 부모 노드의 왼쪽 노드일 때 + min = Math.min(min, minTree[b]); + max = Math.max(max, maxTree[b]); + b--; + } + a >>= 1; // 다음 부모 노드로 올라감 + b >>= 1; + } + return new Node(min, max); + } + } + + private static class Node{ + int min, max; + Node(int min, int max){ + this.min = min; + this.max = max; + } + } + + public static void main(String[] args) throws IOException { + int N = readInt(); + int M = readInt(); // 쿼리 개수 + + SegTree st = new SegTree(N); + + for (int i=0;i0; i--) { + arr[i].min = Math.min(arr[i*2].min, arr[i*2+1].min); + arr[i].max = Math.max(arr[i*2].max, arr[i*2+1].max); + } + } + + // [a, b] 구간의 최댓값, 최솟값 조회 + Node getMinMax(int a, int b){ + a += start; + b += start; + int min = Integer.MAX_VALUE; + int max = Integer.MIN_VALUE; + + while (a <= b){ // a 와 b 가 겹칠 때까지 왼쪽/오른쪽에서 가운데로 좁혀오도록 이동 (최고 높이의 공통 부모 노드까지 이동) + // 구간을 쪼갰을 때 양쪽 경계에 걸쳐 있는 예외 노드를 따로 챙겨주자 + if ((a&1)==1){ // (1) a가 부모 노드의 오른쪽 노드일 때 + min = Math.min(min, arr[a].min); + max = Math.max(max, arr[a].max); + a++; + } + if ((b&1)==0){ // (2) b가 부모 노드의 왼쪽 노드일 때 + min = Math.min(min, arr[b].min); + max = Math.max(max, arr[b].max); + b--; + } + a >>= 1; // 다음 부모 노드로 올라감 + b >>= 1; + } + return new Node(min, max); + } + } + + private static class Node{ + int min, max; + Node(){ + min = Integer.MAX_VALUE; + max = Integer.MIN_VALUE; + } + Node(int min, int max){ + this.min = min; + this.max = max; + } + } + + public static void main(String[] args) throws IOException { + int N = readInt(); + int M = readInt(); // 쿼리 개수 + + SegTree st = new SegTree(N); + + for (int i=0;i stack = new ArrayDeque<>(); + for (int i=0;i0 && stack.peekLast() 0) stack.pollLast(); + StringBuilder sb = new StringBuilder(); + for (char s : stack) sb.append(s); + System.out.print(sb); + } } diff --git a/week18/Baekjoon_17619.java b/week18/Baekjoon_17619.java new file mode 100644 index 0000000..76ae6f2 --- /dev/null +++ b/week18/Baekjoon_17619.java @@ -0,0 +1,63 @@ +import java.util.*; +import java.io.*; + +/** + * 25240 KB, 456 ms + */ + +public class Main { + + private static class Log implements Comparable{ + int x1, x2, y, idx; + Log(int x1, int x2, int y, int idx){ + this.x1 = x1; + this.x2 = x2; + this.y = y; + this.idx = idx; + } + + @Override + public int compareTo(Log o) { + return Integer.compare(this.x1, o.x1); + } + } + + public static void main(String[] args) throws IOException { + int N = readInt(); // 통나무 개수 + int Q = readInt(); // 질문 개수 + + Log[] logs = new Log[N]; + for (int i=0;i0) dp[i] = (dp[i] + dp[i-1]) % MOD; + + // 두자리 + int ten = (num.charAt(i-2) - '0') * 10 + one; + if (10<=ten && ten<=26) dp[i] = (dp[i] + dp[i-2]) % MOD; + } + + return dp[n]; + } +} diff --git a/week18/Baekjoon_2170.java b/week18/Baekjoon_2170.java new file mode 100644 index 0000000..f23caea --- /dev/null +++ b/week18/Baekjoon_2170.java @@ -0,0 +1,55 @@ +import java.util.*; +import java.io.*; + +/** + * 52712 KB, 1120 ms + */ + +public class Main { + + private static class Line implements Comparable{ + int start, end; + + public Line(int start, int end) { + this.start = start; + this.end = end; + } + + @Override + public int compareTo(Line o) { + return Integer.compare(this.start, o.start); + } + } + + public static void main(String[] args) throws IOException { + int N = readInt(); + PriorityQueue pq = new PriorityQueue<>(); + + for (int i=0;i end){ + result += cur.end - end; + end = cur.end; + } else if (cur.start >= end){ + result += cur.end - cur.start; + end = cur.end; + } + } + + System.out.print(result); + + } + + private static final StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); + private static int readInt() throws IOException{ + st.nextToken(); + return (int) st.nval; + } +} diff --git a/week20/Baekjoon1464.java b/week20/Baekjoon1464.java index e078aef..570f585 100644 --- a/week20/Baekjoon1464.java +++ b/week20/Baekjoon1464.java @@ -1,3 +1,27 @@ -public class Baekjoon1516 { +package m07.d12; + +/** + * JAVA8 ) 메모리: 11_608 KB, 시간: 60ms + */ + +import java.io.*; +import java.util.*; + +public class Main_1464_뒤집기3 { + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String S = br.readLine(); + ArrayDeque q = new ArrayDeque<>(); + q.offer(S.charAt(0)); + for (int i=1;i people, fire; + private static boolean[][] isVisited; // 사람 방문여부 + private static int R, C, time = 1; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + R = Integer.parseInt(st.nextToken()); + C = Integer.parseInt(st.nextToken()); + + map = new char[R][C]; + isVisited = new boolean[R][C]; + people = new ArrayDeque<>(); + fire = new ArrayDeque<>(); + + for (int i=0;i0){ + int[] cur = fire.poll(); + for (int i=0;i<4;i++){ + int nx = cur[0] + dx[i]; + int ny = cur[1] + dy[i]; + if (nx<0||ny<0||nx>=R||ny>=C) continue; + if (map[nx][ny]=='F'||map[nx][ny]=='#') continue; + + map[nx][ny] = 'F'; + fire.offer(new int[]{nx, ny}); + } + } + + // 사람 이동 + while (ps-->0){ + int[] cur = people.poll(); + + for (int i=0;i<4;i++){ + int nx = cur[0] + dx[i]; + int ny = cur[1] + dy[i]; + if (nx<0||ny<0||nx>=R||ny>=C) return true; + if (map[nx][ny]=='F'||map[nx][ny]=='#') continue; + if (isVisited[nx][ny]) continue; + + isVisited[nx][ny] = true; + people.offer(new int[]{nx, ny}); + } + } + + time++; + } + + return false; + } }