SHOW:
|
|
- or go back to the newest paste.
1 | // Author : Saurav Kalsoor | |
2 | - | // Sort By Indices - JAVA |
2 | + | // Sort By Indices - KOTLIN |
3 | ||
4 | - | import java.util.*; |
4 | + | import java.util.* |
5 | ||
6 | - | public class Test { |
6 | + | var sc: Scanner = Scanner(System.`in`) |
7 | ||
8 | - | static Scanner sc = new Scanner(System.in); |
8 | + | fun main() { |
9 | - | public static void main(String[] args) { |
9 | + | val n: Int = sc.nextInt() |
10 | - | int n = sc.nextInt(); |
10 | + | val m: Int = sc.nextInt() |
11 | - | int m = sc.nextInt(); |
11 | + | val arr: ArrayList<Int> = ArrayList<Int>() |
12 | - | ArrayList<Integer> arr = new ArrayList<>(); |
12 | + | val indices: ArrayList<Int> = ArrayList<Int>() |
13 | - | ArrayList<Integer> indices = new ArrayList<>(); |
13 | + | |
14 | for (i in 0 until n) { | |
15 | - | for(int i=0; i < n; i++) { |
15 | + | val input: Int = sc.nextInt() |
16 | - | int input = sc.nextInt(); |
16 | + | arr.add(input) |
17 | - | arr.add(input); |
17 | + | |
18 | for (i in 0 until m) { | |
19 | val input: Int = sc.nextInt() | |
20 | - | for(int i=0; i < m; i++) { |
20 | + | indices.add(input) |
21 | - | int input = sc.nextInt(); |
21 | + | |
22 | - | indices.add(input); |
22 | + | val result: ArrayList<Int> = sortByIndices(arr, indices, n, m) |
23 | ||
24 | for (i in 0 until n) | |
25 | - | ArrayList<Integer> result = sortByIndices(arr, indices, n, m); |
25 | + | print(result.get(i).toString() + " ") |
26 | - | |
26 | + | println() |
27 | - | for(int i=0; i < n; i++) |
27 | + | |
28 | - | System.out.print(result.get(i) + " "); |
28 | + | |
29 | - | |
29 | + | fun sortByIndices(arr: ArrayList<Int>, indices: ArrayList<Int>, n: Int, m: Int): ArrayList<Int> { |
30 | - | System.out.println(); |
30 | + | val sortDesc: ArrayList<Int> = ArrayList<Int>() |
31 | val sortAsc: ArrayList<Int> = ArrayList<Int>() | |
32 | val indicesSet: HashSet<Int> = HashSet<Int>() | |
33 | - | public static ArrayList<Integer> sortByIndices(ArrayList<Integer> arr, ArrayList<Integer> indices, int n, int m){ |
33 | + | |
34 | - | ArrayList<Integer> sortDesc = new ArrayList<Integer>(); |
34 | + | for (i in 0 until m) { |
35 | - | ArrayList<Integer> sortAsc = new ArrayList<Integer>(); |
35 | + | indicesSet.add(indices.get(i)) |
36 | } | |
37 | - | HashSet<Integer> indicesSet = new HashSet<Integer>(); |
37 | + | |
38 | for (i in 0 until n) { | |
39 | - | for(int i=0; i < m; i++){ |
39 | + | if (indicesSet.contains(i)) { |
40 | - | indicesSet.add(indices.get(i)); |
40 | + | sortDesc.add(arr.get(i)) |
41 | } else { | |
42 | sortAsc.add(arr.get(i)) | |
43 | - | for(int i=0; i < n; i++){ |
43 | + | |
44 | - | if(indicesSet.contains(i)){ |
44 | + | |
45 | - | sortDesc.add(arr.get(i)); |
45 | + | |
46 | - | }else{ |
46 | + | Collections.sort(sortDesc, Collections.reverseOrder()) |
47 | - | sortAsc.add(arr.get(i)); |
47 | + | Collections.sort(sortAsc) |
48 | - | } |
48 | + | |
49 | val result: ArrayList<Int> = ArrayList<Int>() | |
50 | ||
51 | - | Collections.sort(sortDesc, Collections.reverseOrder()); |
51 | + | var j = 0 |
52 | - | Collections.sort(sortAsc); |
52 | + | var k = 0 |
53 | ||
54 | - | ArrayList<Integer> result = new ArrayList<Integer>(); |
54 | + | for (i in 0 until n) { |
55 | if (indicesSet.contains(i)) { | |
56 | - | int j = 0, k = 0; |
56 | + | result.add(sortDesc.get(j)) |
57 | - | for(int i=0; i < n; i++){ |
57 | + | j++ |
58 | - | if(indicesSet.contains(i)){ |
58 | + | } else { |
59 | - | result.add(sortDesc.get(j)); |
59 | + | result.add(sortAsc.get(k)) |
60 | - | j++; |
60 | + | k++ |
61 | - | }else{ |
61 | + | |
62 | - | result.add(sortAsc.get(k)); |
62 | + | |
63 | - | k++; |
63 | + | return result |
64 | - | } |
64 | + | } |