File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ public static void main (String [] args ) throws IOException {
7+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
8+ StringTokenizer st;
9+
10+ int n = Integer . parseInt(br. readLine());
11+
12+ ArrayList<Integer > list = new ArrayList<> ();
13+
14+ st = new StringTokenizer (br. readLine());
15+ for (int i = 0 ; i < n; i++ ) {
16+ list. add(Integer . parseInt(st. nextToken()));
17+ }
18+
19+ int s = Integer . parseInt(br. readLine());
20+
21+ for (int i = 0 ; i < n && s > 0 ; i++ ) {
22+ int maxVal = - 1 ;
23+ int maxIdx = - 1 ;
24+
25+ int limit = Math . min(i + s, n - 1 );
26+
27+ for (int j = i; j <= limit; j++ ) {
28+ if (list. get(j) > maxVal) {
29+ maxVal = list. get(j);
30+ maxIdx = j;
31+ }
32+ }
33+
34+ list. remove(maxIdx);
35+ list. add(i, maxVal);
36+
37+ s -= (maxIdx - i);
38+ }
39+
40+ StringBuilder sb = new StringBuilder ();
41+ for (int num : list) {
42+ sb. append(num). append(" " );
43+ }
44+ System . out. println(sb. toString());
45+ }
46+ }
47+ ```
You can’t perform that action at this time.
0 commit comments