aboutsummaryrefslogtreecommitdiff
path: root/shuffle.java
blob: bcabaf820114323b55be8c04cefebf9438441ceb (plain)
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
50
51
import java.io.*;
import java.util.*;
public class shuffle {
	public static void main(String[] args) throws IOException {
		// initialize file I/O
		BufferedReader br = new BufferedReader(new FileReader("shuffle.in"));
		PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("shuffle.out")));
		
		// read in the number of cows
		int n = Integer.parseInt(br.readLine());
		
		// if a cow was in position i after shuffling, then moveTo[i] will
		// be the location that they were in before the shuffle
		int[] moveTo = new int[n+1];
		StringTokenizer st = new StringTokenizer(br.readLine());
		for(int i = 1; i <= n; i++) {
			// destination is the location a cow would be after a shuffle
			// if they were originally in position i
			int destination = Integer.parseInt(st.nextToken());;
			moveTo[destination] = i;
		}
		
		// allocate an array to store the observed locations of all cows
		// read in the observations
		int[] finalLocs = new int[n+1];
		st = new StringTokenizer(br.readLine());
		for(int i = 1; i <= n; i++) {
			finalLocs[i] = Integer.parseInt(st.nextToken());
		}

		br.close();
		
		// allocate an array to store the original locations of all cows
		int[] originalLocations = new int[n+1];
		for(int finalPosition = 1; finalPosition <= n; finalPosition++) {
			int currentLocation = finalPosition;
			// reverse three shuffles
			for(int iter = 1; iter <= 3; iter++) {
				currentLocation = moveTo[currentLocation];
			}
			// store the original location of the cow that ended up in finalPosition
			originalLocations[currentLocation] = finalLocs[finalPosition];
		}
		
		// print the answer
		for(int i = 1; i <= n; i++) {
			pw.println(originalLocations[i]);
		}
		pw.close();
	}
}