I participated in IVY Topcoder 2011 and submitted my code
Question:Musical Water Fountains
Andhra Pradesh tourism is planning to construct a park and install water fountains. It is planning to organize "Musical Fountain" shows.
The fountains shoot water in air in varying directions and with varying heights.
Fountains are to be installed in a row such that water from all the fountains is visible.
You are given int[] height, int[] turnOnTime, and int[] turnOffTime.
height represents how high each fountain shoots water in air, turnOnTime represents the time when fountain shoots the water in air and
turnOffTime represents the time when fountain stops shooting the water. Musical fountain show would be for 30 minutes.
Elements in turnOnTime and turnOffTime will be a number between 1 and 30 inclusive and turnOffTime [i] will always be greater than turnOnTime [i].
Fountain that shoots water high in air is to be installed as far as possible. The fountain that shoots water with less height must be installed in
front of the fountain that shoots water high in air to prevent blocking. A fountain can start shooting water and other stops at the same minute but
still one can block the other. You should also try to have the fountain with maximum possible height being first in the park.
You should return a int[] which contains the elements of height in the order you should install fountains to achieve the above goal.
The front of the park is represented by the first element in your return value, and is where you view the fountains from. The elements in height []
will all be unique
Definition
Class: MusicalFountain
Method: getFountainOrdering
Parameters: int[], int[], int[]
Returns: int[]
Method signature: int[]getFountainOrdering (int[] height, int[] turnOnTime, int[] turnOffTime) (be sure your method is public)
Constraints
height will have between 2 and 10 unique elements, inclusive.
turnOnTime and turnOffTime will have the same number of elements as height.
Each element of height will be between 1 and 30 meters, inclusive.
Each element of turnOnTime and turnOffTime will be between 1 and 30 minutes, inclusive.
For each element i of turnOnTime and turnOffTime, turnOffTime [i] will be greater than turnOnTime [i].
Example 1
height[] = {6, 5, 4, 3, 2, 1}
turnOnTime [] = {1, 1, 1, 1, 1, 1}
turnOffTime [] = {30, 30, 30, 30, 30, 30}
Returns: {1, 2, 3, 4, 5, 6}
All fountains start at 1st minute and stop at 30th minute. All will block each other; you must order them from shortest to tallest.
Example 1
height[] = {6, 5, 4, 3, 2, 1}
turnOnTime [] = {1, 5, 10, 15, 20, 25}
turnOffTime [] = {4, 9, 14, 19, 24, 29}
Returns: {6, 5, 4, 3, 2, 1}
The fountains start and stop at different times so they will never block each other. You can order them from tallest to shortest.
Question:Musical Water Fountains
Andhra Pradesh tourism is planning to construct a park and install water fountains. It is planning to organize "Musical Fountain" shows.
The fountains shoot water in air in varying directions and with varying heights.
Fountains are to be installed in a row such that water from all the fountains is visible.
You are given int[] height, int[] turnOnTime, and int[] turnOffTime.
height represents how high each fountain shoots water in air, turnOnTime represents the time when fountain shoots the water in air and
turnOffTime represents the time when fountain stops shooting the water. Musical fountain show would be for 30 minutes.
Elements in turnOnTime and turnOffTime will be a number between 1 and 30 inclusive and turnOffTime [i] will always be greater than turnOnTime [i].
Fountain that shoots water high in air is to be installed as far as possible. The fountain that shoots water with less height must be installed in
front of the fountain that shoots water high in air to prevent blocking. A fountain can start shooting water and other stops at the same minute but
still one can block the other. You should also try to have the fountain with maximum possible height being first in the park.
You should return a int[] which contains the elements of height in the order you should install fountains to achieve the above goal.
The front of the park is represented by the first element in your return value, and is where you view the fountains from. The elements in height []
will all be unique
Definition
Class: MusicalFountain
Method: getFountainOrdering
Parameters: int[], int[], int[]
Returns: int[]
Method signature: int[]getFountainOrdering (int[] height, int[] turnOnTime, int[] turnOffTime) (be sure your method is public)
Constraints
height will have between 2 and 10 unique elements, inclusive.
turnOnTime and turnOffTime will have the same number of elements as height.
Each element of height will be between 1 and 30 meters, inclusive.
Each element of turnOnTime and turnOffTime will be between 1 and 30 minutes, inclusive.
For each element i of turnOnTime and turnOffTime, turnOffTime [i] will be greater than turnOnTime [i].
Example 1
height[] = {6, 5, 4, 3, 2, 1}
turnOnTime [] = {1, 1, 1, 1, 1, 1}
turnOffTime [] = {30, 30, 30, 30, 30, 30}
Returns: {1, 2, 3, 4, 5, 6}
All fountains start at 1st minute and stop at 30th minute. All will block each other; you must order them from shortest to tallest.
Example 1
height[] = {6, 5, 4, 3, 2, 1}
turnOnTime [] = {1, 5, 10, 15, 20, 25}
turnOffTime [] = {4, 9, 14, 19, 24, 29}
Returns: {6, 5, 4, 3, 2, 1}
The fountains start and stop at different times so they will never block each other. You can order them from tallest to shortest.
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"); } }
No comments:
Post a Comment