From e78aca1808294ea178e8176782ecf429f3ad649d Mon Sep 17 00:00:00 2001 From: zinnnn37 Date: Mon, 15 Sep 2025 22:34:41 +0900 Subject: [PATCH] =?UTF-8?q?[20250915]=20BOJ=20/=20G4=20/=20=EC=86=8C?= =?UTF-8?q?=ED=8A=B8=20/=20=EA=B9=80=EB=AF=BC=EC=A7=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../15 BOJ G4 \354\206\214\355\212\270.md" | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 "zinnnn37/202509/15 BOJ G4 \354\206\214\355\212\270.md" diff --git "a/zinnnn37/202509/15 BOJ G4 \354\206\214\355\212\270.md" "b/zinnnn37/202509/15 BOJ G4 \354\206\214\355\212\270.md" new file mode 100644 index 00000000..c0099457 --- /dev/null +++ "b/zinnnn37/202509/15 BOJ G4 \354\206\214\355\212\270.md" @@ -0,0 +1,74 @@ +```java +import java.io.*; +import java.util.StringTokenizer; + +public class BJ_1083_소트 { + + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static StringTokenizer st; + + private static int N, S; + private static int[] nums; + + public static void main(String[] args) throws IOException { + init(); + sol(); + } + + private static void init() throws IOException { + N = Integer.parseInt(br.readLine()); + + nums = new int[N]; + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < N; i++) { + nums[i] = Integer.parseInt(st.nextToken()); + } + + S = Integer.parseInt(br.readLine()); + } + + private static void sol() throws IOException { + if (N == 1) { + print(); + return; + } + + int idx = 0; + while (S > 0 && idx < N) { + int maxIdx = idx; + + // find max + for (int i = idx; i < N; i++) { + if (i > idx + S) break; + + if (nums[i] > nums[maxIdx]) { + maxIdx = i; + } + } + for (int j = maxIdx; j > idx; j--) { + swap(j, j - 1); + S--; + } + idx++; + } + print(); + } + + private static void swap(int a, int b) { + int tmp = nums[a]; + nums[a] = nums[b]; + nums[b] = tmp; + } + + private static void print() throws IOException { + for (int i = 0; i < N; i++) { + bw.write(nums[i] + " "); + } + bw.flush(); + bw.close(); + br.close(); + } + +} +``` \ No newline at end of file