diff --git a/binary_search/12015/nayoung12015.java b/binary_search/12015/nayoung12015.java new file mode 100644 index 0000000..d7125d1 --- /dev/null +++ b/binary_search/12015/nayoung12015.java @@ -0,0 +1,46 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +// 풀이 참고 : https://st-lab.tistory.com/285 + +public class nayoung12015 { + 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[] A = new int[N]; + + for (int i = 0; i < N; i++) { + A[i] = Integer.parseInt(st.nextToken()); + } + + int[] LIS = new int[N]; + LIS[0] = A[0]; + int lengthOfLIS = 1; + + for (int i = 1; i < N; i++) { + if (LIS[lengthOfLIS - 1] < A[i]) { + LIS[lengthOfLIS] = A[i]; + lengthOfLIS++; + } else { + int lo = 0; + int hi = lengthOfLIS; + + while (lo < hi) { + int mid = (lo + hi) / 2; + + if (LIS[mid] <= A[i]) lo = mid + 1; + else hi = mid; + } + + LIS[lo] = A[i]; + } + } + + System.out.println(lengthOfLIS); + } +} diff --git a/binary_search/1300/nayoung1300.java b/binary_search/1300/nayoung1300.java new file mode 100644 index 0000000..c7752cf --- /dev/null +++ b/binary_search/1300/nayoung1300.java @@ -0,0 +1,31 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +// 풀이 참고 : https://st-lab.tistory.com/281 + +public class nayoung1300 { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + int N = Integer.parseInt(br.readLine()); + int k = Integer.parseInt(br.readLine()); + + long low = 1; + long high = k; + + while (low < high) { + long mid = (low + high) / 2; + long count = 0; + + for (int i = 1; i <= N; i++) { + count += Math.min(mid / i, N); + } + + if (k <= count) high = mid; + else low = mid + 1; + } + + System.out.println(low); + } +} diff --git a/binary_search/1920/nayoung1920.java b/binary_search/1920/nayoung1920.java new file mode 100644 index 0000000..fb8c1eb --- /dev/null +++ b/binary_search/1920/nayoung1920.java @@ -0,0 +1,49 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.StringTokenizer; + +public class nayoung1920 { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st; + + int N = Integer.parseInt(br.readLine()); + int[] A = new int[N]; + + st = new StringTokenizer(br.readLine()); + + for (int i = 0; i < N; i++) { + A[i] = Integer.parseInt(st.nextToken()); + } + + Arrays.sort(A); + + int M = Integer.parseInt(br.readLine()); + st = new StringTokenizer(br.readLine()); + + for (int i = 0; i < M; i++) { + int m = Integer.parseInt(st.nextToken()); + if (isInArr(A, m)) { + System.out.println(1); + } else { + System.out.println(0); + } + } + } + + public static boolean isInArr(int[] A, int m) { + int min = 0, max = A.length - 1; + + while (min <= max) { + int mid = (min + max) / 2; + + if (A[mid] == m) return true; + else if (A[mid] < m) min = mid + 1; + else max = mid - 1; + } + + return false; + } +} diff --git a/binary_search/19637/nayoung19637.java b/binary_search/19637/nayoung19637.java new file mode 100644 index 0000000..1147f8a --- /dev/null +++ b/binary_search/19637/nayoung19637.java @@ -0,0 +1,56 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +public class nayoung19637 { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + StringTokenizer st = new StringTokenizer(br.readLine()); + + StringBuilder sb = new StringBuilder(); + + int N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + + List items = new ArrayList<>(); + + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + String name = st.nextToken(); + int value = Integer.parseInt(st.nextToken()); + + items.add(new Item(name, value)); + } + + for (int i = 0; i < M; i++) { + int power = Integer.parseInt(br.readLine()); + + int low = 0; + int high = N-1; + + while (low <= high) { + int mid = (low + high) / 2; + + if (power <= items.get(mid).value) high = mid - 1; + else low = mid + 1; + } + + sb.append(items.get(high+1).name).append('\n'); + } + + System.out.println(sb); + + } + + static class Item { + String name; + int value; + + public Item(String name, int value) { + this.name = name; + this.value = value; + } + } +}