From 330abbcf078dacfb23974e6695d4add99e64e7af Mon Sep 17 00:00:00 2001 From: Dltmd202 Date: Wed, 27 Jul 2022 21:15:34 +0900 Subject: [PATCH 1/2] Feat: Add solution of #118 --- src/BaekJoon/Dltmd202/10845/Main.java | 48 +++++++++++++++++++++++++++ src/BaekJoon/Dltmd202/10866/Main.java | 44 ++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/BaekJoon/Dltmd202/10845/Main.java create mode 100644 src/BaekJoon/Dltmd202/10866/Main.java diff --git a/src/BaekJoon/Dltmd202/10845/Main.java b/src/BaekJoon/Dltmd202/10845/Main.java new file mode 100644 index 0000000..92ca57d --- /dev/null +++ b/src/BaekJoon/Dltmd202/10845/Main.java @@ -0,0 +1,48 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.LinkedList; +import java.util.Queue; + +/** + * 큐 + * https://www.acmicpc.net/problem/10845 + */ +public class Main { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringBuilder sb = new StringBuilder(); + + public static void main(String[] args) throws IOException { + int n = Integer.parseInt(br.readLine()); + Queue q = new LinkedList<>(); + + for (int i = 0; i < n; i++) { + String command = br.readLine(); + if (command.startsWith("push")){ + q.offer(Integer.parseInt(command.substring(5))); + } else { + if(command.equals("pop")){ + if(!q.isEmpty()) { + sb.append(q.poll()); + } else { + sb.append(-1); + } + } else if(command.equals("size")){ + sb.append(q.size()); + } else if(command.equals("empty")){ + sb.append(q.isEmpty() ? 1: 0); + } else if(command.equals("front")){ + if(!q.isEmpty()){ + sb.append(q.peek()); + } else { + sb.append(-1); + } + } else if(command.equals("back")){ + sb.append(q.stream().reduce((a, b) -> b).orElse(-1)); + } + sb.append("\n"); + } + } + System.out.println(sb.toString().trim()); + } +} diff --git a/src/BaekJoon/Dltmd202/10866/Main.java b/src/BaekJoon/Dltmd202/10866/Main.java new file mode 100644 index 0000000..7032ef1 --- /dev/null +++ b/src/BaekJoon/Dltmd202/10866/Main.java @@ -0,0 +1,44 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.Deque; + +/** + * 덱 + * https://www.acmicpc.net/problem/10866 + */ +public class Main { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringBuilder sb = new StringBuilder(); + + public static void main(String[] args) throws IOException { + int n = Integer.parseInt(br.readLine()); + Deque deque = new ArrayDeque<>(); + + for (int i = 0; i < n; i++) { + String command = br.readLine(); + if (command.startsWith("push_back")){ + deque.offerLast(Integer.parseInt(command.substring(10))); + } else if(command.startsWith("push_front")){ + deque.offerFirst(Integer.parseInt(command.substring(11))); + } else { + if(command.equals("pop_front")){ + sb.append(deque.isEmpty() ? -1 : deque.pollFirst()); + } else if(command.equals("pop_back")){ + sb.append(deque.isEmpty() ? -1 : deque.pollLast()); + } else if(command.equals("size")){ + sb.append(deque.size()); + } else if(command.equals("empty")){ + sb.append(deque.isEmpty() ? 1: 0); + } else if(command.equals("front")){ + sb.append(deque.isEmpty() ? -1 : deque.peekFirst()); + } else if(command.equals("back")){ + sb.append(deque.isEmpty() ? -1 : deque.peekLast()); + } + sb.append("\n"); + } + } + System.out.println(sb.toString().trim()); + } +} From 977d76484593259d0fc659df71319fbfa1dc0e67 Mon Sep 17 00:00:00 2001 From: Dltmd202 Date: Sat, 30 Jul 2022 02:35:04 +0900 Subject: [PATCH 2/2] Feat: Add solution of #118 --- src/BaekJoon/Dltmd202/10989/Main.java | 28 +++++++ src/BaekJoon/Dltmd202/11050/Main.java | 30 ++++++++ src/BaekJoon/Dltmd202/11650/Main.java | 58 +++++++++++++++ src/BaekJoon/Dltmd202/11651/Main.java | 58 +++++++++++++++ src/BaekJoon/Dltmd202/11866/Main.java | 34 +++++++++ src/BaekJoon/Dltmd202/15829/Main.java | 27 +++++++ src/BaekJoon/Dltmd202/18111/Main.java | 101 ++++++++++++++++++++++++++ 7 files changed, 336 insertions(+) create mode 100644 src/BaekJoon/Dltmd202/10989/Main.java create mode 100644 src/BaekJoon/Dltmd202/11050/Main.java create mode 100644 src/BaekJoon/Dltmd202/11650/Main.java create mode 100644 src/BaekJoon/Dltmd202/11651/Main.java create mode 100644 src/BaekJoon/Dltmd202/11866/Main.java create mode 100644 src/BaekJoon/Dltmd202/15829/Main.java create mode 100644 src/BaekJoon/Dltmd202/18111/Main.java diff --git a/src/BaekJoon/Dltmd202/10989/Main.java b/src/BaekJoon/Dltmd202/10989/Main.java new file mode 100644 index 0000000..5662f90 --- /dev/null +++ b/src/BaekJoon/Dltmd202/10989/Main.java @@ -0,0 +1,28 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; + +/** + * 수 정렬하기 3 + * https://www.acmicpc.net/problem/10989 + */ +public class Main { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringBuilder sb = new StringBuilder(); + + public static void main(String[] args) throws IOException{ + int n = Integer.parseInt(br.readLine()); + int[] data = new int[n]; + + for (int i = 0; i < n; i++) { + data[i] = Integer.parseInt(br.readLine()); + } + Arrays.sort(data); + + for (int i = 0; i < n; i++) { + sb.append(data[i]).append("\n"); + } + System.out.println(sb.toString().trim()); + } +} diff --git a/src/BaekJoon/Dltmd202/11050/Main.java b/src/BaekJoon/Dltmd202/11050/Main.java new file mode 100644 index 0000000..0357cb2 --- /dev/null +++ b/src/BaekJoon/Dltmd202/11050/Main.java @@ -0,0 +1,30 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class Main { + static StringBuilder sb = new StringBuilder(); + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + /** + * 이항 계수 1 + * https://www.acmicpc.net/problem/11050 + */ + public static void main(String[] args) throws IOException{ + StringTokenizer st = new StringTokenizer(br.readLine()); + + int n = Integer.parseInt(st.nextToken()); + int k = Integer.parseInt(st.nextToken()); + int[] factorial = new int[n + 1]; + + factorial[0] = 1; + factorial[1] = 1; + for (int i = 2; i <= n; i++) { + factorial[i] = factorial[i - 1] * i; + } + + System.out.println(factorial[n] / (factorial[n - k] * factorial[k])); + + } +} diff --git a/src/BaekJoon/Dltmd202/11650/Main.java b/src/BaekJoon/Dltmd202/11650/Main.java new file mode 100644 index 0000000..9162a7e --- /dev/null +++ b/src/BaekJoon/Dltmd202/11650/Main.java @@ -0,0 +1,58 @@ +import java.io.*; +import java.util.*; +import java.util.stream.*; + +/** + * 좌표 정렬하기1 + * https://www.acmicpc.net/problem/11650 + */ +public class Main { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringBuilder sb = new StringBuilder(); + + public static void main(String[] args) throws IOException{ + int n = Integer.parseInt(br.readLine()); + Coordinate[] coordinates = new Coordinate[n]; + + for (int i = 0; i < n; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + coordinates[i] = new Coordinate( + Integer.parseInt(st.nextToken()), + Integer.parseInt(st.nextToken()) + ); + } + + String answer = Arrays.stream(coordinates) + .sorted(Comparator.comparingInt(Coordinate::getX) + .thenComparing( + Comparator.comparing(Coordinate::getY))) + .map(String::valueOf) + .collect(Collectors.joining("\n")); + + System.out.println(answer); + } + + + static class Coordinate{ + int x; + int y; + + public Coordinate(int x, int y){ + this.y = y; + this.x = x; + } + + public int getY(){ + return y; + } + + public int getX(){ + return x; + } + + @Override + public String toString(){ + return x + " " + y; + } + } +} diff --git a/src/BaekJoon/Dltmd202/11651/Main.java b/src/BaekJoon/Dltmd202/11651/Main.java new file mode 100644 index 0000000..c11333e --- /dev/null +++ b/src/BaekJoon/Dltmd202/11651/Main.java @@ -0,0 +1,58 @@ +import java.io.*; +import java.util.*; +import java.util.stream.*; + +/** + * 좌표 정렬하기2 + * https://www.acmicpc.net/problem/11651 + */ +public class Main { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringBuilder sb = new StringBuilder(); + + public static void main(String[] args) throws IOException{ + int n = Integer.parseInt(br.readLine()); + Coordinate[] coordinates = new Coordinate[n]; + + for (int i = 0; i < n; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + coordinates[i] = new Coordinate( + Integer.parseInt(st.nextToken()), + Integer.parseInt(st.nextToken()) + ); + } + + String answer = Arrays.stream(coordinates) + .sorted(Comparator.comparingInt(Coordinate::getY) + .thenComparing( + Comparator.comparing(Coordinate::getX))) + .map(String::valueOf) + .collect(Collectors.joining("\n")); + + System.out.println(answer); + } + + + static class Coordinate{ + int x; + int y; + + public Coordinate(int x, int y){ + this.y = y; + this.x = x; + } + + public int getY(){ + return y; + } + + public int getX(){ + return x; + } + + @Override + public String toString(){ + return x + " " + y; + } + } +} diff --git a/src/BaekJoon/Dltmd202/11866/Main.java b/src/BaekJoon/Dltmd202/11866/Main.java new file mode 100644 index 0000000..d2cf9eb --- /dev/null +++ b/src/BaekJoon/Dltmd202/11866/Main.java @@ -0,0 +1,34 @@ +import java.util.*; +import java.io.*; +import java.util.stream.*; + +public class Main { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringBuilder sb = new StringBuilder();; + + public static void main(String[] args) throws IOException{ + StringTokenizer st = new StringTokenizer(br.readLine()); + + int n = Integer.parseInt(st.nextToken()); + int k = Integer.parseInt(st.nextToken()); + + List q = IntStream.range(1, n + 1) + .boxed() + .collect(Collectors.toList()); + List answer = new ArrayList<>(); + + int idx = 0; + while (q.size() > 1){ + idx = (idx + (k - 1)) % q.size(); + + answer.add(q.remove(idx)); + } + answer.add(q.remove(0)); + + System.out.println( + answer.stream() + .map(String::valueOf) + .collect(Collectors.joining(", ", "<", ">")) + ); + } +} diff --git a/src/BaekJoon/Dltmd202/15829/Main.java b/src/BaekJoon/Dltmd202/15829/Main.java new file mode 100644 index 0000000..091b709 --- /dev/null +++ b/src/BaekJoon/Dltmd202/15829/Main.java @@ -0,0 +1,27 @@ +import java.io.*; + +/** + * Hashing + * https://www.acmicpc.net/problem/15829 + */ +public class Main { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringBuilder sb = new StringBuilder(); + static long MOD = 1234567891; + + public static void main(String[] args) throws IOException{ + int n = Integer.parseInt(br.readLine()); + + char[] plain = br.readLine().toCharArray(); + long res = 0; + long pow = 1; + + for (int i = 0; i < n; i++) { + res += ((plain[i] - 'a' + 1) * pow); + pow = (pow * 31) % MOD; + } + + System.out.println(res % MOD); + + } +} diff --git a/src/BaekJoon/Dltmd202/18111/Main.java b/src/BaekJoon/Dltmd202/18111/Main.java new file mode 100644 index 0000000..e3eeb2e --- /dev/null +++ b/src/BaekJoon/Dltmd202/18111/Main.java @@ -0,0 +1,101 @@ +import java.util.*; +import java.io.*; + +/** + * 마인크래프트 + * https://www.acmicpc.net/problem/18111 + */ +public class Main { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringBuilder sb = new StringBuilder(); + + public static void main(String[] args) throws IOException{ + StringTokenizer st = new StringTokenizer(br.readLine()); + + Minecraft.n = Integer.parseInt(st.nextToken()); + Minecraft.m = Integer.parseInt(st.nextToken()); + Minecraft.baseLeft = Integer.parseInt(st.nextToken()); + Minecraft.map = new int[Minecraft.n][Minecraft.m]; + Minecraft answer = null; + int maxHeight = Integer.MIN_VALUE; + int minHeight = Integer.MAX_VALUE; + + for (int i = 0; i < Minecraft.n; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 0; j < Minecraft.m; j++) { + Minecraft.map[i][j] = Integer.parseInt(st.nextToken()); + maxHeight = Math.max(maxHeight, Minecraft.map[i][j]); + minHeight = Math.min(minHeight, Minecraft.map[i][j]); + } + } + + for (int i = minHeight; i <= maxHeight; i++) { + Minecraft minecraft = new Minecraft(i); + minecraft.flat(); + if(Objects.isNull(answer) || answer.compareTo(minecraft) > 0){ + answer = minecraft; + } + } + + System.out.println(answer); + } + + static class Minecraft implements Comparable{ + static int n; + static int m; + static int[][] map; + static int baseLeft; + int left; + int time; + int height = 0; + + public Minecraft(int height) { + this.height = height; + left = Minecraft.baseLeft; + this.time = 0; + } + + void flat(){ + actionFlat(height, (i, j, h) -> { + int gap = map[i][j] - h; + time += (gap * 2); + left += gap; + }, (i, j, h) -> { + int gap = h - map[i][j]; + time += gap; + left -= gap; + }); + + if(left < 0) time = Integer.MAX_VALUE; + } + + private void actionFlat(int height, TriConsumer higher, + TriConsumer lower) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if(map[i][j] > height) higher.apply(i, j, height); + else lower.apply(i, j, height); + } + } + } + + @Override + public int compareTo(Minecraft o) { + if(Integer.compare(this.time, o.time) == 0){ + return Integer.compare(this.height, o.height); + } else { + return Integer.compare(this.time, o.time); + } + } + + @Override + public String toString() { + return time + " " + height; + } + } + + @FunctionalInterface + interface TriConsumer{ + void apply(T t, U u, V v); + } +}