-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ1083.java
More file actions
49 lines (43 loc) · 1.44 KB
/
BOJ1083.java
File metadata and controls
49 lines (43 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package ¹éÁØ;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class BOJ1083 {
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 S = Integer.parseInt(br.readLine());
ArrayList<Integer> arr = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for(int i = 0; i < N; i++) {
arr.add(Integer.parseInt(st.nextToken()));
}
int moveCnt = 0, changeIdx = 0;
while (moveCnt < S && changeIdx < N - 1) {
int maxNum = arr.get(changeIdx), maxIdx = -1;
int idx = changeIdx + 1, count = 1;
while (moveCnt + count <= S && idx < N) {
int num = arr.get(idx);
if (num > maxNum) {
maxNum = num;
maxIdx = idx;
}
count++;
idx++;
}
if (maxIdx != -1) {
arr.remove(maxIdx);
arr.add(changeIdx, maxNum);
moveCnt += maxIdx - changeIdx;
}
changeIdx++;
}
for(int i = 0; i < N; i++) {
sb.append(arr.get(i)).append(" ");
}
System.out.print(sb);
}
}