From 74580924267b31c4b7d5afcc029ddf7c5de3ab41 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Fri, 20 Jun 2025 22:22:02 +0900 Subject: [PATCH] =?UTF-8?q?[20250620]=20BOJ=20/=20P5=20/=20=EA=B0=80?= =?UTF-8?q?=EC=9E=A5=20=EA=B8=B4=20=EC=A6=9D=EA=B0=80=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EB=B6=80=EB=B6=84=20=EC=88=98=EC=97=B4=20/=20=EC=9D=B4?= =?UTF-8?q?=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\353\266\204 \354\210\230\354\227\264.md" | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 "lkhyun/202506/20 BOJ P5 \352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264.md" diff --git "a/lkhyun/202506/20 BOJ P5 \352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264.md" "b/lkhyun/202506/20 BOJ P5 \352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264.md" new file mode 100644 index 00000000..830f003c --- /dev/null +++ "b/lkhyun/202506/20 BOJ P5 \352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264.md" @@ -0,0 +1,75 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + static int N; + static int[] arr; + static List LIS; + static int[] lisLen; + static int[] realIdx; + + public static void main(String[] args) throws IOException { + N = Integer.parseInt(br.readLine()); + arr = new int[N+1]; + LIS = new ArrayList<>(); + lisLen = new int[N+1]; + realIdx = new int[N+1]; + + st = new StringTokenizer(br.readLine()); + for (int i = 1; i <= N; i++) { + int cur = Integer.parseInt(st.nextToken()); + arr[i] = cur; + + if(LIS.isEmpty() || LIS.get(LIS.size()-1) < cur){ + LIS.add(cur); + lisLen[i] = LIS.size();//맨 뒤에 붙이는 경우는 LIS길이만큼 만들 수 있는거 + realIdx[LIS.size()-1] = i; + } else { + int pos = binarySearch(cur); + LIS.set(pos, cur); + lisLen[i] = pos + 1; //LIS는 0-base니까 1더해줌. 이진 탐색으로 찾은 위치까지 LIS를 만들 수 있다는 의미. + realIdx[pos] = i; + } + } + + bw.write(LIS.size() + "\n"); + + List result = new ArrayList<>(); + int targetLength = LIS.size(); + + for (int i = N; i >= 1; i--) { + if (lisLen[i] == targetLength) { + result.add(arr[i]); + targetLength--; + } + } + + Collections.reverse(result); + + for (int i : result) { + bw.write(i + " "); + } + bw.write("\n"); + + bw.close(); + } + + public static int binarySearch(int target) { + int left = 0; int right = LIS.size() - 1; + + while(left < right){ + int mid = (left + right) / 2; + if(LIS.get(mid) < target){ + left = mid + 1; + } else { + right = mid; + } + } + return left; + } +} +```