From d3f4ce26ee21ab584de062edffc75a80cb56d26f Mon Sep 17 00:00:00 2001 From: woong7 <39939886+woong7@users.noreply.github.com> Date: Tue, 26 Jul 2022 01:53:19 +0900 Subject: [PATCH 01/23] Create java_20922_panda.java --- .../20922/java_20922_panda.java" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 "5\354\243\274\354\260\250/20922/java_20922_panda.java" diff --git "a/5\354\243\274\354\260\250/20922/java_20922_panda.java" "b/5\354\243\274\354\260\250/20922/java_20922_panda.java" new file mode 100644 index 00000000..330f92a4 --- /dev/null +++ "b/5\354\243\274\354\260\250/20922/java_20922_panda.java" @@ -0,0 +1,40 @@ +package algorithm_study.week5; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; +import java.util.Queue; +import java.util.StringTokenizer; + +public class java_20922_panda { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int K = Integer.parseInt(st.nextToken()); + + int max = 0; + Map counts = new HashMap<>(); + Queue sub = new LinkedList<>(); + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < N; i++) { + int num = Integer.parseInt(st.nextToken()); + sub.add(num); + int count = counts.getOrDefault(num, 0); + counts.put(num, ++count); + if (count > K) { + int head = sub.poll(); + while (head != num) { + counts.replace(head, counts.get(head) - 1); + head = sub.poll(); + } + counts.replace(num, K); + } + max = Math.max(sub.size(), max); + } + System.out.println(max); + } +} From 1882ceb3f7949988af09648ebb7d1ec5660975b8 Mon Sep 17 00:00:00 2001 From: woong7 <39939886+woong7@users.noreply.github.com> Date: Tue, 26 Jul 2022 01:53:36 +0900 Subject: [PATCH 02/23] Delete java_5904_panda.java --- .../5904/java_5904_panda.java" | 37 ------------------- 1 file changed, 37 deletions(-) delete mode 100644 "5\354\243\274\354\260\250/5904/java_5904_panda.java" diff --git "a/5\354\243\274\354\260\250/5904/java_5904_panda.java" "b/5\354\243\274\354\260\250/5904/java_5904_panda.java" deleted file mode 100644 index 3389efb7..00000000 --- "a/5\354\243\274\354\260\250/5904/java_5904_panda.java" +++ /dev/null @@ -1,37 +0,0 @@ -package algorithm_study.week5; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.Stack; - -public class java_5904_panda { - static Stack mooDigits = new Stack<>(); - - public static void main(String[] args) throws IOException { - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - long N = Long.parseLong(br.readLine()); - mooDigits.push(0L); - long length = 3; - for (int step = 0; step < 32; step++) { - mooDigits.push(length); - if (length >= N) break; - length = 2 * length + step + 4; - } - System.out.println(calculateMoo(N - 1)); - } - - private static String calculateMoo(long index) { - long subSize = mooDigits.peek(); - if (index == 0 || index == subSize) - return "m"; - if (index < 3 || (index > subSize && index < subSize + mooDigits.size() + 3)) - return "o"; - - if (index > subSize) { - index -= (subSize + mooDigits.size() + 3); - } - mooDigits.pop(); - return calculateMoo(index); - } -} From 9aca05bea21c60ef860c3aee7237836f9d760d67 Mon Sep 17 00:00:00 2001 From: woong7 <39939886+woong7@users.noreply.github.com> Date: Tue, 26 Jul 2022 01:53:45 +0900 Subject: [PATCH 03/23] Delete java_6198_panda.java --- .../6198/java_6198_panda.java" | 25 ------------------- 1 file changed, 25 deletions(-) delete mode 100644 "5\354\243\274\354\260\250/6198/java_6198_panda.java" diff --git "a/5\354\243\274\354\260\250/6198/java_6198_panda.java" "b/5\354\243\274\354\260\250/6198/java_6198_panda.java" deleted file mode 100644 index 5e1871de..00000000 --- "a/5\354\243\274\354\260\250/6198/java_6198_panda.java" +++ /dev/null @@ -1,25 +0,0 @@ -package algorithm_study.week5; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.Stack; - -public class java_6198_panda { - public static void main(String[] args) throws IOException { - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - int N = Integer.parseInt(br.readLine()); - Stack stack = new Stack<>(); - stack.push(Integer.MAX_VALUE); - long sum = 0; - for (int i = 0; i < N; i++) { - int height = Integer.parseInt(br.readLine()); - while (stack.peek() <= height) { - stack.pop(); - } - sum += stack.size(); - stack.push(height); - } - System.out.println(sum); - } -} From 95f3dfcc40bdbcded01f2f21cdfeef18832e0ef4 Mon Sep 17 00:00:00 2001 From: woong7 <39939886+woong7@users.noreply.github.com> Date: Tue, 26 Jul 2022 01:54:06 +0900 Subject: [PATCH 04/23] Create java_5904_panda_1.java --- .../5904/java_5904_panda_1.java" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 "5\354\243\274\354\260\250/5904/java_5904_panda_1.java" diff --git "a/5\354\243\274\354\260\250/5904/java_5904_panda_1.java" "b/5\354\243\274\354\260\250/5904/java_5904_panda_1.java" new file mode 100644 index 00000000..3389efb7 --- /dev/null +++ "b/5\354\243\274\354\260\250/5904/java_5904_panda_1.java" @@ -0,0 +1,37 @@ +package algorithm_study.week5; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Stack; + +public class java_5904_panda { + static Stack mooDigits = new Stack<>(); + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + long N = Long.parseLong(br.readLine()); + mooDigits.push(0L); + long length = 3; + for (int step = 0; step < 32; step++) { + mooDigits.push(length); + if (length >= N) break; + length = 2 * length + step + 4; + } + System.out.println(calculateMoo(N - 1)); + } + + private static String calculateMoo(long index) { + long subSize = mooDigits.peek(); + if (index == 0 || index == subSize) + return "m"; + if (index < 3 || (index > subSize && index < subSize + mooDigits.size() + 3)) + return "o"; + + if (index > subSize) { + index -= (subSize + mooDigits.size() + 3); + } + mooDigits.pop(); + return calculateMoo(index); + } +} From 7266c80e0bdda68a35a40540fd80939c459375b1 Mon Sep 17 00:00:00 2001 From: woong7 <39939886+woong7@users.noreply.github.com> Date: Tue, 26 Jul 2022 01:54:26 +0900 Subject: [PATCH 05/23] Create java_6198_panda_1.java --- .../java_6198_panda_1.java" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 "5\354\243\274\354\260\250/java_6198_panda_1.java" diff --git "a/5\354\243\274\354\260\250/java_6198_panda_1.java" "b/5\354\243\274\354\260\250/java_6198_panda_1.java" new file mode 100644 index 00000000..5e1871de --- /dev/null +++ "b/5\354\243\274\354\260\250/java_6198_panda_1.java" @@ -0,0 +1,25 @@ +package algorithm_study.week5; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Stack; + +public class java_6198_panda { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int N = Integer.parseInt(br.readLine()); + Stack stack = new Stack<>(); + stack.push(Integer.MAX_VALUE); + long sum = 0; + for (int i = 0; i < N; i++) { + int height = Integer.parseInt(br.readLine()); + while (stack.peek() <= height) { + stack.pop(); + } + sum += stack.size(); + stack.push(height); + } + System.out.println(sum); + } +} From 43e3174f5ae9668c038583e9c06a6651b9671244 Mon Sep 17 00:00:00 2001 From: woong7 <39939886+woong7@users.noreply.github.com> Date: Tue, 26 Jul 2022 01:54:40 +0900 Subject: [PATCH 06/23] =?UTF-8?q?Rename=205=EC=A3=BC=EC=B0=A8/java=5F6198?= =?UTF-8?q?=5Fpanda=5F1.java=20to=205=EC=A3=BC=EC=B0=A8/6198/java=5F6198?= =?UTF-8?q?=5Fpanda=5F1.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../6198/java_6198_panda_1.java" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "5\354\243\274\354\260\250/java_6198_panda_1.java" => "5\354\243\274\354\260\250/6198/java_6198_panda_1.java" (100%) diff --git "a/5\354\243\274\354\260\250/java_6198_panda_1.java" "b/5\354\243\274\354\260\250/6198/java_6198_panda_1.java" similarity index 100% rename from "5\354\243\274\354\260\250/java_6198_panda_1.java" rename to "5\354\243\274\354\260\250/6198/java_6198_panda_1.java" From 30be19f384356777e2ecb160bbbf67e752661fa0 Mon Sep 17 00:00:00 2001 From: woong7 <39939886+woong7@users.noreply.github.com> Date: Tue, 26 Jul 2022 01:55:34 +0900 Subject: [PATCH 07/23] Create java_5107_panda.java --- .../5107/java_5107_panda.java" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 "5\354\243\274\354\260\250/5107/java_5107_panda.java" diff --git "a/5\354\243\274\354\260\250/5107/java_5107_panda.java" "b/5\354\243\274\354\260\250/5107/java_5107_panda.java" new file mode 100644 index 00000000..4684656f --- /dev/null +++ "b/5\354\243\274\354\260\250/5107/java_5107_panda.java" @@ -0,0 +1,42 @@ +package algorithm_study.week5; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.StringTokenizer; + +public class java_5107_panda { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringBuilder sb = new StringBuilder(); + int testNo = 1; + while (true) { + int N = Integer.parseInt(br.readLine()); + if (N == 0) break; + + Map adjacency = new HashMap<>(); + for (int i = 0; i < N; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + adjacency.put(st.nextToken(), st.nextToken()); + } + List names = new ArrayList<>(adjacency.keySet()); + + int count = 0; + while(!names.isEmpty()){ + String starting = names.get(0); + String node = starting; + do{ + names.remove(node); + node = adjacency.get(node); + }while(!node.equals(starting)); + count++; + } + sb.append(testNo++).append(" ").append(count).append("\n"); + } + System.out.println(sb); + } +} From 344658f0cbbb24644ae14ab042459c81194c7a8b Mon Sep 17 00:00:00 2001 From: woong7 <39939886+woong7@users.noreply.github.com> Date: Tue, 26 Jul 2022 03:03:36 +0900 Subject: [PATCH 08/23] Create java_16432_panda.java --- .../16432/java_16432_panda.java" | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 "5\354\243\274\354\260\250/16432/java_16432_panda.java" diff --git "a/5\354\243\274\354\260\250/16432/java_16432_panda.java" "b/5\354\243\274\354\260\250/16432/java_16432_panda.java" new file mode 100644 index 00000000..249f464e --- /dev/null +++ "b/5\354\243\274\354\260\250/16432/java_16432_panda.java" @@ -0,0 +1,63 @@ +package algorithm_study.week5; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.StringTokenizer; + +public class java_16432_panda { + static int N; + static int[] picked; + static List[] cakes; + static boolean[][] visited; + static boolean succeed; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + picked = new int[N]; + visited = new boolean[N][]; + + cakes = new ArrayList[N]; + for (int i = 0; i < N; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int m = Integer.parseInt(st.nextToken()); + visited[i] = new boolean[m]; + cakes[i] = new ArrayList<>(); + for (int j = 0; j < m; j++) { + cakes[i].add(Integer.parseInt(st.nextToken())); + } + } + + doDFS(0, 0); + + StringBuilder answer = new StringBuilder(); + if (succeed) { + Arrays.stream(picked).forEachOrdered(it -> answer.append(it).append("\n")); + } else { + answer.append(-1); + } + System.out.println(answer); + } + + private static void doDFS(int day, int last) { + if (day == N) { + succeed = true; + return; + } + + for (int cake : cakes[day]) { + if (cake == last) continue; + int index = cakes[day].indexOf(cake); + if (visited[day][index]) continue; + + visited[day][index] = true; + picked[day] = cake; + doDFS(day + 1, cake); + if (succeed) return; + } + } +} From 15a67357dddbdeffcb0d033b7eda5118a57022be Mon Sep 17 00:00:00 2001 From: woong7 <39939886+woong7@users.noreply.github.com> Date: Tue, 26 Jul 2022 03:07:20 +0900 Subject: [PATCH 09/23] Update java_5904_panda_1.java --- "5\354\243\274\354\260\250/5904/java_5904_panda_1.java" | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git "a/5\354\243\274\354\260\250/5904/java_5904_panda_1.java" "b/5\354\243\274\354\260\250/5904/java_5904_panda_1.java" index 3389efb7..a5d9cc1a 100644 --- "a/5\354\243\274\354\260\250/5904/java_5904_panda_1.java" +++ "b/5\354\243\274\354\260\250/5904/java_5904_panda_1.java" @@ -1,8 +1,6 @@ package algorithm_study.week5; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.util.Stack; public class java_5904_panda { From 18cff3bdafe73a5bcfaca0691a170afd5b33adb4 Mon Sep 17 00:00:00 2001 From: woong7 <39939886+woong7@users.noreply.github.com> Date: Tue, 26 Jul 2022 03:08:38 +0900 Subject: [PATCH 10/23] Create java_5904_panda.java --- .../5904/java_5904_panda.java" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 "5\354\243\274\354\260\250/5904/java_5904_panda.java" diff --git "a/5\354\243\274\354\260\250/5904/java_5904_panda.java" "b/5\354\243\274\354\260\250/5904/java_5904_panda.java" new file mode 100644 index 00000000..3389efb7 --- /dev/null +++ "b/5\354\243\274\354\260\250/5904/java_5904_panda.java" @@ -0,0 +1,37 @@ +package algorithm_study.week5; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Stack; + +public class java_5904_panda { + static Stack mooDigits = new Stack<>(); + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + long N = Long.parseLong(br.readLine()); + mooDigits.push(0L); + long length = 3; + for (int step = 0; step < 32; step++) { + mooDigits.push(length); + if (length >= N) break; + length = 2 * length + step + 4; + } + System.out.println(calculateMoo(N - 1)); + } + + private static String calculateMoo(long index) { + long subSize = mooDigits.peek(); + if (index == 0 || index == subSize) + return "m"; + if (index < 3 || (index > subSize && index < subSize + mooDigits.size() + 3)) + return "o"; + + if (index > subSize) { + index -= (subSize + mooDigits.size() + 3); + } + mooDigits.pop(); + return calculateMoo(index); + } +} From 53d816c61d95c98bb4550d7fcdffced3b5576b1c Mon Sep 17 00:00:00 2001 From: woong7 <39939886+woong7@users.noreply.github.com> Date: Tue, 26 Jul 2022 03:09:24 +0900 Subject: [PATCH 11/23] Create java_6198_panda.java --- .../6198/java_6198_panda.java" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 "5\354\243\274\354\260\250/6198/java_6198_panda.java" diff --git "a/5\354\243\274\354\260\250/6198/java_6198_panda.java" "b/5\354\243\274\354\260\250/6198/java_6198_panda.java" new file mode 100644 index 00000000..5e1871de --- /dev/null +++ "b/5\354\243\274\354\260\250/6198/java_6198_panda.java" @@ -0,0 +1,25 @@ +package algorithm_study.week5; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Stack; + +public class java_6198_panda { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int N = Integer.parseInt(br.readLine()); + Stack stack = new Stack<>(); + stack.push(Integer.MAX_VALUE); + long sum = 0; + for (int i = 0; i < N; i++) { + int height = Integer.parseInt(br.readLine()); + while (stack.peek() <= height) { + stack.pop(); + } + sum += stack.size(); + stack.push(height); + } + System.out.println(sum); + } +} From f9d5f09dda001181cadc29a67c7e99e71c34c16c Mon Sep 17 00:00:00 2001 From: woong7 <39939886+woong7@users.noreply.github.com> Date: Tue, 26 Jul 2022 03:26:24 +0900 Subject: [PATCH 12/23] Create java_11582_panda.java --- .../11582/java_11582_panda.java" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 "5\354\243\274\354\260\250/11582/java_11582_panda.java" diff --git "a/5\354\243\274\354\260\250/11582/java_11582_panda.java" "b/5\354\243\274\354\260\250/11582/java_11582_panda.java" new file mode 100644 index 00000000..f3e89c51 --- /dev/null +++ "b/5\354\243\274\354\260\250/11582/java_11582_panda.java" @@ -0,0 +1,27 @@ +package algorithm_study.week5; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.StringTokenizer; + +public class java_11582_panda { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int N = Integer.parseInt(br.readLine()); + StringTokenizer st = new StringTokenizer(br.readLine()); + int K = Integer.parseInt(br.readLine()); + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < K; i++) { + int[] sub = new int[N / K]; + for (int j = 0; j < N / K; j++) { + sub[j] = Integer.parseInt(st.nextToken()); + } + Arrays.sort(sub); + Arrays.stream(sub).forEach(it->sb.append(it).append(" ")); + } + System.out.println(sb); + } +} From 2200ad77ac8f7169eada189c0dc1e4af6787b6c1 Mon Sep 17 00:00:00 2001 From: Gunwoong Kim <39939886+woong7@users.noreply.github.com> Date: Sat, 6 Aug 2022 21:57:34 +0900 Subject: [PATCH 13/23] Create java_25391_panda.java --- .../14938/java_25391_panda.java" | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 "6\354\243\274\354\260\250/14938/java_25391_panda.java" diff --git "a/6\354\243\274\354\260\250/14938/java_25391_panda.java" "b/6\354\243\274\354\260\250/14938/java_25391_panda.java" new file mode 100644 index 00000000..a5d2d565 --- /dev/null +++ "b/6\354\243\274\354\260\250/14938/java_25391_panda.java" @@ -0,0 +1,39 @@ +package algorithm_study.week6; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +public class java_25391_panda { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + int K = Integer.parseInt(st.nextToken()); + + List students = new ArrayList<>(); + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + students.add(new int[] {Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())}); + } + + long sum = 0; + + students.sort((o1, o2) -> o2[1] - o1[1]); + for (int i = 0; i < K; i++) { + sum += students.remove(0)[0]; + } + + students.sort((o1, o2) -> o2[0] - o1[0]); + for (int i = 0; i < M; i++) { + sum += students.get(i)[0]; + } + + System.out.println(sum); + } +} + From 9e6770479c1346644c6dc72929cc6ddad47d3609 Mon Sep 17 00:00:00 2001 From: Gunwoong Kim <39939886+woong7@users.noreply.github.com> Date: Sat, 6 Aug 2022 21:57:56 +0900 Subject: [PATCH 14/23] Update and rename java_25391_panda.java to java_14938_panda.java --- .../14938/java_14938_panda.java" | 55 +++++++++++++++++++ .../14938/java_25391_panda.java" | 39 ------------- 2 files changed, 55 insertions(+), 39 deletions(-) create mode 100644 "6\354\243\274\354\260\250/14938/java_14938_panda.java" delete mode 100644 "6\354\243\274\354\260\250/14938/java_25391_panda.java" diff --git "a/6\354\243\274\354\260\250/14938/java_14938_panda.java" "b/6\354\243\274\354\260\250/14938/java_14938_panda.java" new file mode 100644 index 00000000..f23580cc --- /dev/null +++ "b/6\354\243\274\354\260\250/14938/java_14938_panda.java" @@ -0,0 +1,55 @@ +package algorithm_study.week6; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class java_14938_panda { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + int n = Integer.parseInt(st.nextToken()); + int m = Integer.parseInt(st.nextToken()); + int r = Integer.parseInt(st.nextToken()); + + int[] items = new int[n]; + int[][] map = new int[n][n]; + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < n; i++) { + items[i] = Integer.parseInt(st.nextToken()); + for (int j = 0; j < n; j++) { + map[i][j] = 10000; + } + map[i][i] = 0; + } + + for (int i = 0; i < r; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()) - 1; + int b = Integer.parseInt(st.nextToken()) - 1; + map[b][a] = map[a][b] = Integer.parseInt(st.nextToken()); + } + + for (int node = 0; node < n; node++) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + map[i][j] = Math.min(map[i][j], map[i][node] + map[node][j]); + } + } + } + + int max = 0; + for (int i = 0; i < n; i++) { + int sum = 0; + for (int j = 0; j < n; j++) { + if (map[i][j] <= m) { + sum += items[j]; + } + } + max = Math.max(max, sum); + } + System.out.println(max); + } +} diff --git "a/6\354\243\274\354\260\250/14938/java_25391_panda.java" "b/6\354\243\274\354\260\250/14938/java_25391_panda.java" deleted file mode 100644 index a5d2d565..00000000 --- "a/6\354\243\274\354\260\250/14938/java_25391_panda.java" +++ /dev/null @@ -1,39 +0,0 @@ -package algorithm_study.week6; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.List; -import java.util.StringTokenizer; - -public class java_25391_panda { - public static void main(String[] args) throws IOException { - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - StringTokenizer st = new StringTokenizer(br.readLine()); - int N = Integer.parseInt(st.nextToken()); - int M = Integer.parseInt(st.nextToken()); - int K = Integer.parseInt(st.nextToken()); - - List students = new ArrayList<>(); - for (int i = 0; i < N; i++) { - st = new StringTokenizer(br.readLine()); - students.add(new int[] {Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())}); - } - - long sum = 0; - - students.sort((o1, o2) -> o2[1] - o1[1]); - for (int i = 0; i < K; i++) { - sum += students.remove(0)[0]; - } - - students.sort((o1, o2) -> o2[0] - o1[0]); - for (int i = 0; i < M; i++) { - sum += students.get(i)[0]; - } - - System.out.println(sum); - } -} - From 8ef56ea4e6d5320c6a7d822f8af3220802203b4b Mon Sep 17 00:00:00 2001 From: Gunwoong Kim <39939886+woong7@users.noreply.github.com> Date: Sat, 6 Aug 2022 21:58:56 +0900 Subject: [PATCH 15/23] Create java_24229_panda.java --- .../24229/java_24229_panda.java" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 "6\354\243\274\354\260\250/24229/java_24229_panda.java" diff --git "a/6\354\243\274\354\260\250/24229/java_24229_panda.java" "b/6\354\243\274\354\260\250/24229/java_24229_panda.java" new file mode 100644 index 00000000..1c2c7dc8 --- /dev/null +++ "b/6\354\243\274\354\260\250/24229/java_24229_panda.java" @@ -0,0 +1,52 @@ +package algorithm_study.week6; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.TreeMap; + +public class java_24229_panda { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int N = Integer.parseInt(br.readLine()); + + List boards = new ArrayList<>(); + + for (int i = 0; i < N; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + boards.add(new int[] {Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())}); + } + + boards.sort(Comparator.comparingInt(o -> o[0])); + + Map connectedBoards = new TreeMap<>(); + int start = boards.get(0)[0]; + int end = boards.get(0)[1]; + for (int i = 1; i < N; i++) { + if (end >= boards.get(i)[0]) { + end = Math.max(end, boards.get(i)[1]); + } else { + connectedBoards.put(start, end); + start = boards.get(i)[0]; + end = boards.get(i)[1]; + } + } + connectedBoards.put(start, end); + + int max = 0; + int endPoint = 0; + for (int key : connectedBoards.keySet()) { + if (max < key) + break; + endPoint = connectedBoards.get(key); + max = Math.max(max, 2 * endPoint - key); + } + + System.out.println(endPoint); + } +} From 80bfd7a69bfaf6df8b00bf32d40a52d76d9cd040 Mon Sep 17 00:00:00 2001 From: Gunwoong Kim <39939886+woong7@users.noreply.github.com> Date: Sat, 6 Aug 2022 21:59:11 +0900 Subject: [PATCH 16/23] Create java_24393_panda.java --- .../24393/java_24393_panda.java" | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 "6\354\243\274\354\260\250/24393/java_24393_panda.java" diff --git "a/6\354\243\274\354\260\250/24393/java_24393_panda.java" "b/6\354\243\274\354\260\250/24393/java_24393_panda.java" new file mode 100644 index 00000000..53d7cfd2 --- /dev/null +++ "b/6\354\243\274\354\260\250/24393/java_24393_panda.java" @@ -0,0 +1,34 @@ +package algorithm_study.week6; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class java_24393_panda { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int N = Integer.parseInt(br.readLine()); + + int position = 1; + for (int i = 0; i < N; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int sum = 0, otherSum=0; + boolean isOnLeft = position <= 13; + position = isOnLeft ? position : position - 13; + + boolean isLeftPick = true; + while (st.hasMoreTokens() && sum < position) { + isLeftPick = !isLeftPick; + int pick = Integer.parseInt(st.nextToken()); + if (isOnLeft != isLeftPick) { + otherSum += pick; + } else { + sum += pick; + } + } + position += otherSum; + } + System.out.println(position); + } +} From e635af997d86ade92b08a91b62989a0ba9a598d1 Mon Sep 17 00:00:00 2001 From: Gunwoong Kim <39939886+woong7@users.noreply.github.com> Date: Sat, 6 Aug 2022 21:59:27 +0900 Subject: [PATCH 17/23] Create java_25391_panda.java --- .../25391/java_25391_panda.java" | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 "6\354\243\274\354\260\250/25391/java_25391_panda.java" diff --git "a/6\354\243\274\354\260\250/25391/java_25391_panda.java" "b/6\354\243\274\354\260\250/25391/java_25391_panda.java" new file mode 100644 index 00000000..a5d2d565 --- /dev/null +++ "b/6\354\243\274\354\260\250/25391/java_25391_panda.java" @@ -0,0 +1,39 @@ +package algorithm_study.week6; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +public class java_25391_panda { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + int K = Integer.parseInt(st.nextToken()); + + List students = new ArrayList<>(); + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + students.add(new int[] {Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())}); + } + + long sum = 0; + + students.sort((o1, o2) -> o2[1] - o1[1]); + for (int i = 0; i < K; i++) { + sum += students.remove(0)[0]; + } + + students.sort((o1, o2) -> o2[0] - o1[0]); + for (int i = 0; i < M; i++) { + sum += students.get(i)[0]; + } + + System.out.println(sum); + } +} + From a8c552c5bf8ff8031ab9201b725676d3ba871362 Mon Sep 17 00:00:00 2001 From: Gunwoong Kim <39939886+woong7@users.noreply.github.com> Date: Sat, 13 Aug 2022 17:59:09 +0900 Subject: [PATCH 18/23] Create java_13905_panda.java --- .../13905/java_13905_panda.java" | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 "7\354\243\274\354\260\250/13905/java_13905_panda.java" diff --git "a/7\354\243\274\354\260\250/13905/java_13905_panda.java" "b/7\354\243\274\354\260\250/13905/java_13905_panda.java" new file mode 100644 index 00000000..45cab881 --- /dev/null +++ "b/7\354\243\274\354\260\250/13905/java_13905_panda.java" @@ -0,0 +1,53 @@ +package algorithm_study.week7; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.PriorityQueue; +import java.util.StringTokenizer; + +public class java_13905_panda { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + st = new StringTokenizer(br.readLine()); + int s = Integer.parseInt(st.nextToken()); + int e = Integer.parseInt(st.nextToken()); + ArrayList[] edges = new ArrayList[N + 1]; + for (int i = 0; i <= N; i++) { + edges[i] = new ArrayList<>(); + } + + for (int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + int x = Integer.parseInt(st.nextToken()); + int y = Integer.parseInt(st.nextToken()); + int z = Integer.parseInt(st.nextToken()); + edges[x].add(new int[] {y, z}); + edges[y].add(new int[] {x, z}); + } + + PriorityQueue pq = new PriorityQueue<>((o1, o2) -> o2[1] - o1[1]); + + boolean[] visited = new boolean[N + 1]; + int[] weights = new int[N + 1]; + weights[s] = 10_000_000; + pq.add(new int[] {s, 0}); + + while (!pq.isEmpty()) { + int node = pq.poll()[0]; + if (visited[node]) + continue; + visited[node] = true; + + for (int[] edge : edges[node]) { + weights[edge[0]] = Math.max(weights[edge[0]], Math.min(weights[node], edge[1])); + pq.add(edge); + } + } + System.out.println(weights[e]); + } +} From 87620e59e672863137aa757e0baf6af09b7e21d2 Mon Sep 17 00:00:00 2001 From: Gunwoong Kim <39939886+woong7@users.noreply.github.com> Date: Sat, 13 Aug 2022 17:59:30 +0900 Subject: [PATCH 19/23] Create java_24499_panda.java --- .../24499/java_24499_panda.java" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 "7\354\243\274\354\260\250/24499/java_24499_panda.java" diff --git "a/7\354\243\274\354\260\250/24499/java_24499_panda.java" "b/7\354\243\274\354\260\250/24499/java_24499_panda.java" new file mode 100644 index 00000000..d875bfb8 --- /dev/null +++ "b/7\354\243\274\354\260\250/24499/java_24499_panda.java" @@ -0,0 +1,33 @@ +package algorithm_study.week7; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class java_24499_panda { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int K = Integer.parseInt(st.nextToken()); + + int[] pies = new int[N]; + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < N; i++) { + pies[i] = Integer.parseInt(st.nextToken()); + } + + long sum = 0; + for (int i = 0; i < K; i++) { + sum += pies[i]; + } + + long max = sum; + for (int i = K; i < N + K; i++) { + sum += (pies[i % N] - pies[i - K]); + max = Math.max(max, sum); + } + System.out.println(max); + } +} From 97dc6dea759665666812e805f5e3fad2f0f11104 Mon Sep 17 00:00:00 2001 From: Gunwoong Kim <39939886+woong7@users.noreply.github.com> Date: Sat, 13 Aug 2022 17:59:46 +0900 Subject: [PATCH 20/23] Create java_16288_panda.java --- .../16288/java_16288_panda.java" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 "7\354\243\274\354\260\250/16288/java_16288_panda.java" diff --git "a/7\354\243\274\354\260\250/16288/java_16288_panda.java" "b/7\354\243\274\354\260\250/16288/java_16288_panda.java" new file mode 100644 index 00000000..2a768b43 --- /dev/null +++ "b/7\354\243\274\354\260\250/16288/java_16288_panda.java" @@ -0,0 +1,31 @@ +package algorithm_study.week7; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Objects; +import java.util.StringTokenizer; +import java.util.TreeSet; + +public class java_16288_panda { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int K = Integer.parseInt(st.nextToken()); + + TreeSet queueTop = new TreeSet<>(); + + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < N; i++) { + int exit = Integer.parseInt(st.nextToken()); + Integer integer = queueTop.lower(exit); + + if (Objects.nonNull(integer)) { + queueTop.remove(integer); + } + queueTop.add(exit); + } + System.out.println(K >= queueTop.size() ? "YES" : "NO"); + } +} From 2e5dc0c1d5db612dc59a65de524eeed0a7aac646 Mon Sep 17 00:00:00 2001 From: Gunwoong Kim <39939886+woong7@users.noreply.github.com> Date: Sat, 13 Aug 2022 18:09:30 +0900 Subject: [PATCH 21/23] Update java_13905_panda.java --- .../13905/java_13905_panda.java" | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git "a/7\354\243\274\354\260\250/13905/java_13905_panda.java" "b/7\354\243\274\354\260\250/13905/java_13905_panda.java" index 45cab881..96b04cc6 100644 --- "a/7\354\243\274\354\260\250/13905/java_13905_panda.java" +++ "b/7\354\243\274\354\260\250/13905/java_13905_panda.java" @@ -4,6 +4,9 @@ import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.PriorityQueue; import java.util.StringTokenizer; @@ -16,18 +19,15 @@ public static void main(String[] args) throws IOException { st = new StringTokenizer(br.readLine()); int s = Integer.parseInt(st.nextToken()); int e = Integer.parseInt(st.nextToken()); - ArrayList[] edges = new ArrayList[N + 1]; - for (int i = 0; i <= N; i++) { - edges[i] = new ArrayList<>(); - } + Map> edges = new HashMap<>(); for (int i = 0; i < M; i++) { st = new StringTokenizer(br.readLine()); int x = Integer.parseInt(st.nextToken()); int y = Integer.parseInt(st.nextToken()); int z = Integer.parseInt(st.nextToken()); - edges[x].add(new int[] {y, z}); - edges[y].add(new int[] {x, z}); + edges.computeIfAbsent(x, none->new ArrayList<>()).add(new int[] {y, z}); + edges.computeIfAbsent(y, none->new ArrayList<>()).add(new int[] {x, z}); } PriorityQueue pq = new PriorityQueue<>((o1, o2) -> o2[1] - o1[1]); @@ -43,7 +43,7 @@ public static void main(String[] args) throws IOException { continue; visited[node] = true; - for (int[] edge : edges[node]) { + for (int[] edge : edges.getOrDefault(node, new ArrayList<>())) { weights[edge[0]] = Math.max(weights[edge[0]], Math.min(weights[node], edge[1])); pq.add(edge); } From b1f134764e4ff8e0121fa1f89ce0df1182940125 Mon Sep 17 00:00:00 2001 From: Gunwoong Kim <39939886+woong7@users.noreply.github.com> Date: Sat, 13 Aug 2022 18:43:51 +0900 Subject: [PATCH 22/23] Create java_9694_panda.java --- .../9694/java_9694_panda.java" | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 "7\354\243\274\354\260\250/9694/java_9694_panda.java" diff --git "a/7\354\243\274\354\260\250/9694/java_9694_panda.java" "b/7\354\243\274\354\260\250/9694/java_9694_panda.java" new file mode 100644 index 00000000..d65e1be6 --- /dev/null +++ "b/7\354\243\274\354\260\250/9694/java_9694_panda.java" @@ -0,0 +1,86 @@ +package algorithm_study.week7; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.PriorityQueue; +import java.util.Stack; +import java.util.StringTokenizer; + +public class java_9694_panda { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + int T = Integer.parseInt(br.readLine()); + + for (int testCase = 1; testCase <= T; testCase++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + + Map> map = new HashMap<>(); + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + int x = Integer.parseInt(st.nextToken()); + int y = Integer.parseInt(st.nextToken()); + int z = Integer.parseInt(st.nextToken()); + map.computeIfAbsent(x, none -> new ArrayList<>()).add(new int[] {y, z}); + map.computeIfAbsent(y, none -> new ArrayList<>()).add(new int[] {x, z}); + } + + Map distance = new HashMap<>(); + Map before = new HashMap<>(); + boolean[] visited = new boolean[M]; + + for (int i = 0; i < M; i++) { + distance.put(i, Integer.MAX_VALUE); + before.put(i, -1); + } + distance.replace(0, 0); + + PriorityQueue pq = new PriorityQueue<>(Comparator.comparingInt(o -> o[1])); + pq.add(new int[] {0, distance.get(0)}); + + Stack path = new Stack<>(); + + while (!pq.isEmpty()) { + int node = pq.poll()[0]; + + if (visited[node]) + continue; + visited[node] = true; + + if (node == M - 1) { + while (node != -1) { + path.push(node); + node = before.get(node); + } + break; + } + + for (int[] edge : map.getOrDefault(node, new ArrayList<>())) { + if (distance.get(edge[0]) > distance.get(node) + edge[1]) { + distance.replace(edge[0], distance.get(node) + edge[1]); + before.replace(edge[0], node); + pq.add(new int[] {edge[0], distance.get(edge[0])}); + } + } + } + + StringBuilder sb = new StringBuilder("Case #").append(testCase).append(": "); + if (path.isEmpty()) { + System.out.println(sb.append(-1)); + continue; + } + while (!path.isEmpty()) { + sb.append(path.pop()).append(" "); + } + System.out.println(sb.substring(0, sb.length() - 1)); + } + } +} From 0a7062985538a0d9df2785cd85f01682a553888e Mon Sep 17 00:00:00 2001 From: Gunwoong Kim <39939886+woong7@users.noreply.github.com> Date: Sat, 20 Aug 2022 22:59:51 +0900 Subject: [PATCH 23/23] Create java_16498_panda.java --- .../16498/java_16498_panda.java" | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 "8\354\243\274\354\260\250/16498/java_16498_panda.java" diff --git "a/8\354\243\274\354\260\250/16498/java_16498_panda.java" "b/8\354\243\274\354\260\250/16498/java_16498_panda.java" new file mode 100644 index 00000000..9d421013 --- /dev/null +++ "b/8\354\243\274\354\260\250/16498/java_16498_panda.java" @@ -0,0 +1,76 @@ +package algorithm_study.week8; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Objects; +import java.util.StringTokenizer; +import java.util.TreeSet; + +public class java_16498_panda { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + StringTokenizer st = new StringTokenizer(br.readLine()); + int aNum = Integer.parseInt(st.nextToken()); + int bNum = Integer.parseInt(st.nextToken()); + int cNum = Integer.parseInt(st.nextToken()); + + TreeSet A = new TreeSet<>(); + TreeSet B = new TreeSet<>(); + TreeSet C = new TreeSet<>(); + + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < aNum; i++) { + A.add(Integer.parseInt(st.nextToken())); + } + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < bNum; i++) { + B.add(Integer.parseInt(st.nextToken())); + } + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < cNum; i++) { + C.add(Integer.parseInt(st.nextToken())); + } + + int answer = 100_000_000; + + for (int a : A) { + Integer bCeil = B.ceiling(a); + if (Objects.nonNull(bCeil)) { + if (C.subSet(a, true, bCeil, true).isEmpty()) { + Integer cCeil = C.ceiling(bCeil); + if (Objects.nonNull(cCeil)) { + answer = Math.min(answer, cCeil - a); + } + + Integer cFloor = C.floor(a); + if (Objects.nonNull(cFloor)) { + answer = Math.min(answer, bCeil - cFloor); + } + } else { + answer = Math.min(answer, bCeil - a); + } + } + + Integer bFloor = B.floor(a); + if (Objects.nonNull(bFloor)) { + if (C.subSet(bFloor, true, a, true).isEmpty()) { + Integer cCeil = C.ceiling(a); + if(Objects.nonNull(cCeil)){ + answer = Math.min(answer, cCeil - bFloor); + } + + Integer cFloor = C.floor(bFloor); + if(Objects.nonNull(cFloor)){ + answer = Math.min(answer , a - cFloor); + } + + } else { + answer = Math.min(answer, a - bFloor); + } + } + } + System.out.println(answer); + } +}