B. Berland Music
Difference between en2 and en3, changed 130 character(s)
![ ](https://xp.io/storage/b3MoF5n.png)↵
[problem:

import java.lang.*;↵
import java.io.*;↵
import java.util.*;↵

public class B {↵
   static class FastReader {↵
        BufferedReader br;↵
        StringTokenizer st;↵

        public FastReader() {↵
            try {↵
                br = new BufferedReader(↵
                        new FileReader("input.txt"));↵
                PrintStream out = new PrintStream(new FileOutputStream("output.txt"));↵
                System.setOut(out);↵
            } catch (Exception e) {↵
                br = new BufferedReader(new InputStreamReader(System.in));↵
            }↵
        }↵

        String next() {↵
            while (st == null || !st.hasMoreElements()) {↵
                try {↵
                    st = new StringTokenizer(br.readLine());↵
                } catch (IOException e) {↵
                    e.printStackTrace();↵
                }↵
            }↵
            return st.nextToken();↵
        }↵

        int nextInt() {↵
            return Integer.parseInt(next());↵
        }↵

        long nextLong() {↵
            return Long.parseLong(next());↵
        }↵

        double nextDouble() {↵
            return Double.parseDouble(next());↵
        }↵

        String nextLine() {↵
            String str = "";↵
            try {↵
                str = br.readLine();↵
            } catch (IOException e) {↵
                e.printStackTrace();↵
            }↵
            return str;↵
        }↵
    }↵
    static class Rating{↵
     int rat;↵
     int indx;↵
     Rating(int r, int in){↵
     rat = r;↵
     indx = in;↵
     }↵
    }↵
    // end of fast i/o code↵
    public static void main(String[] args) {↵
        FastReader reader = new FastReader();↵
        int t = reader.nextInt();↵
        while(t-->0){↵
         int n = reader.nextInt();↵
         int ratings[] = new int[n];↵
         for(int indx = 0; indx<n; indx++){↵
         ratings[indx] = reader.nextInt();↵
         }↵
         String likeDislike = reader.nextLine();↵
         // System.out.println(likeDislike);↵
         ArrayList<Rating> highRat = new ArrayList<>();↵
         ArrayList<Rating> lowRat = new ArrayList<>();↵
         for(int indx = 0; indx<n; indx++){↵
               char rat = likeDislike.charAt(indx);↵
               if(rat == '0'){↵
                   lowRat.add(new Rating(ratings[indx], indx));↵
               }else{↵
                  highRat.add(new Rating(ratings[indx], indx));↵
               }↵
         }↵
         Collections.sort(highRat, (a, b)->b.rat - a.rat);↵
         Collections.sort(lowRat, (a, b)->b.rat - a.rat);↵
         // for(Rating r : highRat){↵
         //      System.out.print(r.rat+" ");↵
         // }↵
         ↵
         // System.out.println();↵
         int maxRat = n, currRat = maxRat;↵
         for(Rating r : highRat){↵
                ratings[r.indx] = currRat--;↵
         }↵
         for(Rating r : lowRat){↵
                ratings[r.indx] = currRat--;↵
         }↵
         for(int r : ratings){↵
         System.out.print(r+" ");↵
         }↵
         System.out.println();↵
        }↵
    }↵
}]
![ ](https://xp.io/storage/b3MoF5n.png

Any suggestions, corrections, or optimizations to code are welcomed :
)

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English abhishek1102 2021-12-28 14:46:27 3064
en3 English abhishek1102 2021-12-27 21:30:00 130
en2 English abhishek1102 2021-12-27 21:26:45 3079
en1 English abhishek1102 2021-12-27 21:25:02 55 Initial revision (published)