Advertisement
RainLiOfficial

Untitled

Aug 13th, 2022
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class shuffle {
  5.     public static void main(String[] args) throws IOException{
  6.         //BufferedReader file = new BufferedReader(new InputStreamReader(System.in));
  7.         BufferedReader file = new BufferedReader(new FileReader("shuffle.in"));
  8.         PrintWriter outfile = new PrintWriter (new BufferedWriter(new FileWriter("shuffle.out")));
  9.         int n = Integer.parseInt(file.readLine());
  10.         int[] arr = new int[n];
  11.         StringTokenizer st = new StringTokenizer(file.readLine());
  12.         for (int i = 0; i<n; i++){
  13.             arr[i] = Integer.parseInt(st.nextToken());
  14.         }
  15.         StringTokenizer st1 = new StringTokenizer(file.readLine());
  16.         int[] cows = new int[n];
  17.         for (int i = 0; i<n; i++){
  18.             cows[i] = Integer.parseInt(st1.nextToken());
  19.         }
  20.         int[] perm = new int[n]; // perm[i] = x -> initial = i, ending = x = perm[i]
  21.         for (int i = 0; i<n; i++){
  22.             perm[arr[i]-1] = i;
  23.         }
  24.         for (int i = 0; i<3; i++){ // perform 3 swaps
  25.             int[] temp = new int[n]; // the new array that we are constructing
  26.             for (int j = 0; j<perm.length; j++){
  27.                 int init = perm[j];
  28.                 int endingIndex = j;
  29.                 temp[init] = cows[endingIndex];
  30.             }
  31.             cows = temp;
  32.         }
  33.         for (int i : cows){ // for each loop to print the answer
  34.             outfile.println(i);
  35.         }
  36.         outfile.close();
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement