diff --git a/boj/youngsu5582/week2/1461.java b/boj/youngsu5582/week2/1461.java new file mode 100644 index 0000000..93248f6 --- /dev/null +++ b/boj/youngsu5582/week2/1461.java @@ -0,0 +1,64 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Collections; +import java.util.Optional; +import java.util.PriorityQueue; +import java.util.StringTokenizer; + +public class Main { + private static PriorityQueue plus = new PriorityQueue<>(Collections.reverseOrder()); + private static PriorityQueue minus = new PriorityQueue<>(Collections.reverseOrder()); + private static int n; + private static int m; + private static int sum = 0; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine(), " "); + n = Integer.parseInt(st.nextToken()); + m = Integer.parseInt(st.nextToken()); + + st = new StringTokenizer(br.readLine(), " "); + while (st.hasMoreTokens()) { + int number = Integer.parseInt(st.nextToken()); + if (number > 0) { + plus.add(number); + } else { + minus.add(number * -1); + } + } + + int max = findMax(); + add(plus); + add(minus); + + System.out.println(sum - max); + } + + public static int findMax() { + int plus = Optional.ofNullable(Main.plus.peek()) + .orElse(-1); + int minus = Optional.ofNullable(Main.minus.peek()) + .orElse(-1); + return plus > minus ? plus : minus; + } + + public static void add(PriorityQueue pq) { + int count = 0; + int temp = -1; + while (!pq.isEmpty()) { + temp = Math.max(pq.poll(), temp); + count++; + if (count == m) { + count = 0; + sum += temp * 2; + temp = -1; + } + } + if (temp == -1) { + return; + } + sum += temp * 2; + } +} diff --git a/boj/youngsu5582/week2/1890.java b/boj/youngsu5582/week2/1890.java new file mode 100644 index 0000000..24d1346 --- /dev/null +++ b/boj/youngsu5582/week2/1890.java @@ -0,0 +1,58 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.math.BigInteger; +import java.util.Arrays; +import java.util.StringTokenizer; + +public class Main { + private static int[][] list; + private static BigInteger[][] dp; + private static int n; + + public static void main(final String[] args) throws IOException { + final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + + n = Integer.valueOf(reader.readLine()); + + list = new int[n][n]; + dp = new BigInteger[n][n]; + + for (int i = 0; i < n; i++) { + StringTokenizer st = new StringTokenizer(reader.readLine(), " "); + int index = 0; + while (st.hasMoreTokens()) { + list[i][index++] = Integer.valueOf(st.nextToken()); + } + } + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + dp[i][j] = BigInteger.ZERO; + } + } + + dp[0][0] = BigInteger.ONE; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + execute(i, j); + } + } + System.out.println(dp[n - 1][n - 1]); + } + + public static void execute(int x, int y) { + int count = list[x][y]; + if (list[x][y] == 0) { + return; + } + int nextX = x + count; + int nextY = y + count; + if (nextY < n) { + dp[x][nextY] = dp[x][y].add(dp[x][nextY]); + } + if (nextX < n) { + dp[nextX][y] = dp[x][y].add(dp[nextX][y]); + } + } +} diff --git a/boj/youngsu5582/week2/22944.java b/boj/youngsu5582/week2/22944.java new file mode 100644 index 0000000..98bb4e4 --- /dev/null +++ b/boj/youngsu5582/week2/22944.java @@ -0,0 +1,176 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.Map; +import java.util.StringTokenizer; + +public class Main { + private static Field[][] list; + private static int n; + private static int h; + private static int d; + private static int answer = Integer.MAX_VALUE; + private static int[][] visited; + + public static void main(final String[] args) throws IOException { + final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + + StringTokenizer st = new StringTokenizer(reader.readLine(), " "); + + n = Integer.valueOf(st.nextToken()); + h = Integer.valueOf(st.nextToken()); + d = Integer.valueOf(st.nextToken()); + + list = new Field[n][n]; + visited = new int[n][n]; + + int startX = 0; + int startY = 0; + for (int i = 0; i < n; i++) { + String line = reader.readLine(); + for (int j = 0; j < n; j++) { + list[i][j] = Field.from(line.charAt(j)); + if (list[i][j] == Field.START) { + startX = i; + startY = j; + } + } + } + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + visited[i][j] = Integer.MAX_VALUE; + } + } + + Player player = Player.from(h); + player.move(startX, startY); + if (answer == Integer.MAX_VALUE) { + System.out.println(-1); + } else { + System.out.println(answer); + } + } + + private static class Player { + private int hp; + private int umbrella; + private int count; + private Map used; + + public Player(int hp, int count, int umbrella, final Map used) { + this.hp = hp; + this.count = count; + this.umbrella = umbrella; + this.used = new HashMap<>(used); + } + + public static Player from(final int hp) { + return new Player(hp, 0, 0, new HashMap<>()); + } + + + public void move(int x, int y) { + check(x, y); + if (isEnd()) { + return; + } + this.count += 1; + for (Direction direction : Direction.values()) { + int nextX = x + direction.x; + int nextY = y + direction.y; + if (canMove(nextX, nextY) && visited[nextX][nextY] > count) { + visited[nextX][nextY] = count; + new Player(hp, count, umbrella, used).move(nextX, nextY); + } + } + } + + private boolean isEnd() { + if (hp == 0) { + return true; + } + return count >= answer; + } + + private void check(int x, int y) { + Field f = list[x][y]; + switch (f) { + case TOXIC: + moveToxic(); + break; + case END: + moveEnd(); + break; + case UMBRELLA: + moveUmbrella(x, y); + moveToxic(); + break; + default: + } + } + + private void moveUmbrella(int x, int y) { + if (used.containsKey(x * 500 + y)) { + return; + } + used.put(x * 500 + y, true); + umbrella = d; + } + + private void moveToxic() { + if (umbrella > 0) { + this.umbrella -= 1; + return; + } + if (hp > 0) { + this.hp -= 1; + } + } + + private void moveEnd() { + answer = Math.min(answer, count); + } + + private boolean canMove(int x, int y) { + return 0 <= x && x < n && 0 <= y && y < n) + } + } + + private enum Direction { + UP(-1, 0), + RIGHT(0, 1), + DOWN(1, 0), + LEFT(0, -1); + private final int x; + private final int y; + + Direction(final int x, final int y) { + this.x = x; + this.y = y; + } + + } + + private enum Field { + START('S'), + TOXIC('.'), + UMBRELLA('U'), + END('E'); + private final char value; + + Field(final char value) { + this.value = value; + } + + public static Field from(final char value) { + for (final Field field : values()) { + if (field.value == value) { + return field; + } + } + return null; + } + } +} diff --git a/boj/youngsu5582/week2/5567.java b/boj/youngsu5582/week2/5567.java new file mode 100644 index 0000000..517640c --- /dev/null +++ b/boj/youngsu5582/week2/5567.java @@ -0,0 +1,47 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class Main { + private static int[][] list; + private static boolean[] visited; + private static int answer = 0; + + public static void main(final String[] args) throws IOException { + final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + + int n = Integer.parseInt(reader.readLine()); + int m = Integer.parseInt(reader.readLine()); + + list = new int[n + 1][n + 1]; + visited = new boolean[n + 1]; + for (int i = 0; i < m; i++) { + StringTokenizer st = new StringTokenizer(reader.readLine(), " "); + int x = Integer.parseInt(st.nextToken()); + int y = Integer.parseInt(st.nextToken()); + list[x][y] = 1; + list[y][x] = 1; + } + visited[1] = true; + + dfs(1, 2); + System.out.println(answer); + + } + + public static void dfs(int x, int count) { + if (count <= 0) { + return; + } + for (int i = 2; i < list.length; i++) { + if (list[x][i] == 1) { + if (!visited[i]) { + answer++; + visited[i] = true; + } + dfs(i, count - 1); + } + } + } +} diff --git a/boj/youngsu5582/week3/1245.java b/boj/youngsu5582/week3/1245.java new file mode 100644 index 0000000..0b5419c --- /dev/null +++ b/boj/youngsu5582/week3/1245.java @@ -0,0 +1,57 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class Main { + private static final StringBuilder sb = new StringBuilder(); + private static int n; + private static int m; + private static int[][] ary; + private static boolean[][] visited; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + StringTokenizer st = new StringTokenizer(br.readLine(), " "); + n = Integer.parseInt(st.nextToken()); + m = Integer.parseInt(st.nextToken()); + ary = new int[n][m]; + + for (int i = 0; i < n; i++) { + st = new StringTokenizer(br.readLine(), " "); + for (int j = 0; j < m; j++) { + ary[i][j] = Integer.parseInt(st.nextToken()); + } + } + visited = new boolean[n][m]; + int count = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (!visited[i][j] && ary[i][j] != 0) { + visited[i][j] = true; + dfs(i, j); + count++; + } + } + } + System.out.println(count); + } + + private static int[][] directions = new int[][]{ + //RIGHT LEFT DOWN UP + {-1, 0}, {1, 0}, {0, -1}, {0, 1}}; + + private static void dfs(int x, int y) { + for (int i = 0; i < directions.length; i++) { + int newX = x + directions[i][0]; + int newY = y + directions[i][1]; + if (newX >= 0 && newX < n && newY >= 0 && newY < m) { + if (!visited[newX][newY] && ary[x][y] != 0 && (ary[newX][newY] >= ary[x][y] - 1 || ary[newX][newY] <= ary[x][y] + 1)) { + visited[newX][newY] = true; + dfs(newX, newY); + } + } + } + } +} diff --git a/boj/youngsu5582/week3/20167.java b/boj/youngsu5582/week3/20167.java new file mode 100644 index 0000000..abc7aff --- /dev/null +++ b/boj/youngsu5582/week3/20167.java @@ -0,0 +1,50 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class Main { +private static final StringBuilder sb = new StringBuilder(); +private static int n; +private static int k; +private static int[] ary; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + StringTokenizer st = new StringTokenizer(br.readLine(), " "); + n = Integer.parseInt(st.nextToken()); + k = Integer.parseInt(st.nextToken()); + ary = new int[n]; + int count = 0; + st = new StringTokenizer(br.readLine(), " "); + while (st.hasMoreTokens()) { + ary[count++] = Integer.parseInt(st.nextToken()); + } + + count = 0; + int prev = 0; + int result = 0; + while (count < ary.length) { + prev += ary[count++]; + if (prev >= k) { + int next_count = count - 1; + int next_prev = ary[next_count++]; + while (next_count != ary.length && next_prev < k) { + next_prev += ary[next_count++]; + } + if (next_prev > prev) { + count = next_count + 1; + result += next_prev - k; + } else { + result += prev - k; + } + prev = 0; + } + } + if (prev >= k) { + result += prev - k; + } + System.out.println(result); + } +} diff --git a/boj/youngsu5582/week3/4889.java b/boj/youngsu5582/week3/4889.java new file mode 100644 index 0000000..592ddea --- /dev/null +++ b/boj/youngsu5582/week3/4889.java @@ -0,0 +1,55 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.Deque; + +public class Main { + private static final StringBuilder sb = new StringBuilder(); + private static int caseNumber = 0; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + Deque dq = new ArrayDeque<>(); + String line = ""; + while (true) { + line = br.readLine(); + if (line.indexOf("-") != -1) { + break; + } + caseNumber++; + execute(line); + } + } + + private static void execute(String line) { + Deque dq = new ArrayDeque<>(); + for (Character c : line.toCharArray()) { + if (c.equals('{')) { + dq.addLast(1); + } else if (c.equals('}')) { + if (!dq.isEmpty() && dq.peekLast() == 1) { + dq.pollLast(); + } else { + dq.addLast(2); + } + } + } + int count = calculate(dq); + System.out.println(String.format("%d. %d", caseNumber, count)); + } + + private static int calculate(Deque dq) { + int count = 0; + while (!dq.isEmpty()) { + int c1 = dq.pollFirst(); + int c2 = dq.pollFirst(); + if (c1 == 2 && c2 == 1) { + count += 2; + } else { + count += 1; + } + } + return count; + } +} diff --git a/boj/youngsu5582/week3/6236.java b/boj/youngsu5582/week3/6236.java new file mode 100644 index 0000000..73e0512 --- /dev/null +++ b/boj/youngsu5582/week3/6236.java @@ -0,0 +1,62 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +// 현우는 용돈을 효율적으로 활용하고 돈을 펑펑 쓰지 않기 위해서 +// 앞으로 N일 동안 자신이 매일 사용할 금액을 계산하고, +// 정확히 통장에서 M번, K원 씩 출금해서 사용하기로 결정했습니다. +// 현재 수중에 있는 금액으로 하루를 보낼 수 있다면 그대로 소비합니다. +// 부족하다면, 수중에 있는 금액은 통장에 넣은 뒤 K원을 인출해서 하루를 생활합니다. +// 이때, 현우가 매번 출금할 금액 K원의 최솟값을 구하는 프로그램을 작성해주세요. +public class Main { +private static final StringBuilder sb = new StringBuilder(); +private static int n; +private static int m; +private static List ary = new ArrayList<>(); + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + StringTokenizer st = new StringTokenizer(br.readLine(), " "); + n = Integer.parseInt(st.nextToken()); + m = Integer.parseInt(st.nextToken()); + + for (int i = 0; i < n; i++) { + ary.add(Integer.parseInt(br.readLine())); + } + int max = findMax(); + int left = 0; + int right = (max * n) / m; + System.out.println(Math.max(max, findMinK(left, right))); + } + + private static int findMinK(int left, int right) { + int mid = (left + right) / 2; + int money = 0; + int count = 0; + if (left > right) { + return left; + } + for (int i = 0; i < ary.size(); i++) { + if (money < ary.get(i)) { + count++; + money = mid; + if (count == m + 1) { + return findMinK(mid + 1, right); + } + } + money -= ary.get(i); + } + return findMinK(left, mid - 1); + } + + private static int findMax() { + return ary.stream() + .mapToInt(Integer::intValue) + .max() + .orElse(0); + } +}