package topcoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class MusicalFountain {
public static int[]getFountainOrdering (int[] input1, int[] input2, int[] input3){
ArrayList<Integer> result = new ArrayList<Integer>();
arrange(input1, input2, input3);
result.add(input1[0]);
for(int i = 1; i < input1.length; i++){
int newPos = i;
for(int j = i-1; j >= 0; j--){
if(((input2[i] >= input2[j] && input2[i] <= input3[j])
|| (input3[i] >= input2[j] && input3[i] <= input3[j])
|| (input2[j] >= input2[i] && input2[j] <= input3[i])
|| (input3[j] >= input2[i] && input3[j] <= input2[i]))
&& (input1[i] < input1[j])){
int temp = result.indexOf(input1[j]);
newPos = temp < newPos ? temp : newPos;
}/*else if(input1[i] > input1[j]){
int temp = result.indexOf(input1[j]);
newPos = temp < newPos ? temp : newPos;
}*/
}
result.add(newPos, input1[i]);
}
int[] finalResult = new int[input1.length];
for(int i = 0; i < result.size(); i++){
finalResult[i] = result.get(i);
}
return finalResult;
}
public static void arrange(int input1[], int input2[], int input3[]){
ArrayList<Integer> heightList = new ArrayList<Integer>();
for(int i:input1)
heightList.add(i);
Comparator<Integer> reverse = Collections.reverseOrder();
Collections.sort(heightList, reverse);
arrangeDependent(input1, heightList, input2);
arrangeDependent(input1, heightList, input3);
for(int i=0; i<heightList.size(); i++)
input1[i] = heightList.get(i);
}
private static void arrangeDependent(int[] input1, ArrayList<Integer> heightList, int[] data) {
int[] tempData = new int[input1.length];
for(int oldIndex=0; oldIndex<input1.length; oldIndex++){
int newIndex = heightList.indexOf(input1[oldIndex]);
tempData[newIndex] = data[oldIndex];
}
for(int i=0; i<data.length; i++){
data[i] = tempData[i];
}
}
public static void main(String[] args) {
/*int[]height={6, 5, 4, 3, 2, 1};
int turnOnTime [] = {1, 1, 1, 1, 1, 1};
int turnOffTime [] = {30, 30, 30, 30, 30, 30};*/
/*int height[] = {6, 5, 4, 3, 2, 1};
int turnOnTime [] = {1, 5, 10, 15, 20, 25};
int turnOffTime [] = {4, 9, 14, 19, 24, 29};*/
/*int height[] = {6, 5, 4, 3, 2, 1};
int turnOnTime [] = {1, 5, 10, 15, 20, 25};
int turnOffTime [] = {5, 10, 15, 20, 25, 30};*/
/*int height[] = {5, 4, 3, 2, 1, 6};
int turnOnTime [] = {6, 10, 15, 20, 2, 1};
int turnOffTime [] = {9, 15, 20, 25, 5, 6};*/
/*int height[] = {1, 2, 3, 4, 5, 6};
int turnOnTime[] = {1, 16, 1, 16, 1, 16};
int turnOffTime[] = {15, 30, 15, 30, 15, 30};*/
int height[] = {1,3,5,2,4,6};
int turnOnTime[] = {11,25,15,3,21,6};
int turnOffTime[] = {15,30,25,5,25,10};
/*arrange(height, turnOnTime, turnOffTime);
printIntArray(height);
printIntArray(turnOnTime);
printIntArray(turnOffTime);*/
int[] result=getFountainOrdering(height,turnOnTime,turnOffTime);
printIntArray(result);
}
public static void printIntArray(int[] datas){
for(int data:datas)
System.out.println(data);
System.out.println("END");
}
}