diff --git a/solutions/groovy/median_sort.groovy b/solutions/groovy/median_sort.groovy new file mode 100644 index 00000000..befdc37f --- /dev/null +++ b/solutions/groovy/median_sort.groovy @@ -0,0 +1,78 @@ +//Problem to solve: Median Sort + +//Function: getIndex +//Args: list: Actual list of ordered numbers +// a: Number to place in the list +def getIndex(List list, int a){ + //Define reader for the input + def reader = new Scanner(System.in) + int i = Math.floorDiv(list.size(), 3) //Define j as 2/3 from the length of the list + int j = Math.floorDiv(list.size() * 2, 3) //Define j as 2/3 from the length of the list + int inicio = 0, fin = list.size() - 1 //Define beginning and end of the subarray + while (true){ + println(list.get(i) + " " + a + " " + list.get(j)) //Print the 3 values to work with + def res = reader.nextInt() //Read the answer for the median of the 3 values given + if(res == -1) System.exit(-1) //Finish program if answer = -1 + if(res == a){ //Case [0].....[1/3].....[a].....[2/3].....[1] + if(j - i == 1){ + return j + }else{ + inicio = i + fin = j + j = inicio + Math.floorDiv((fin - inicio) * 2, 3) + i = inicio + Math.floorDiv((fin - inicio), 3) + } + }else if( res == list.get(i)){ //Case [0].....[a].....[1/3].....[2/3].....[1] + if(i == 0){ + return 0 + }else if(j - i == 1){ + j = i + i-- + }else{ + fin = i + j = inicio + Math.floorDiv((i - inicio) * 2, 3) + i = inicio + Math.floorDiv((i - inicio), 3) + } + }else{ //Case [0].....[1/3].....[2/3].....[a].....[1] + if(j == list.size() - 1){ + return list.size() + }else if(j - i == 1){ + i = j + j++ + }else{ + inicio = j + i = j + Math.floorDiv((fin - j), 3) + j = j + Math.floorDiv((fin - j) * 2, 3) + } + } + if(i == j) j++ + } +} + +def reader = new Scanner(System.in) //Define reader for the input +def T = reader.nextInt() //Read T = Number of cases +def N = reader.nextInt() //Read N = Length of the list +def Q = reader.nextInt() //Read Q = Max number of queries +//Iteration through all the cases +1.upto(T) {ti-> + println("1 2 3") //Initial question + def l = reader.nextInt() + if(l == -1)System.exit(-1) + def list = [] //Initial list + if(l == 1) list = [2, 1, 3] + else if(l == 2) list = [1, 2, 3] + else list = [1, 3, 2] + while(list.size() < N){ //Iterate every number until the length of the final list + def nextElm = list.size() + 1 + def indx = getIndex(list, nextElm) + list.add(indx, nextElm) + } + String res = "" + for(int i = 0; i < list.size(); i++){ + res = res + list.get(i) + if( i < list.size() -1)res = res + " " + } + println(res) //Print final result + def isOk = reader.nextInt() + if(isOk == -1) System.exit(-1) //If answer is -1, finish the program +} diff --git a/solutions/groovy/moons_and_umbrellas.groovy b/solutions/groovy/moons_and_umbrellas.groovy new file mode 100644 index 00000000..e0098d30 --- /dev/null +++ b/solutions/groovy/moons_and_umbrellas.groovy @@ -0,0 +1,36 @@ +//Problem to solve: Moons and Umbrellas + +class MoonsAndUmbrelas{ + static void main(String[] args) { + //Define reader for the input + def reader = new Scanner(System.in) + //T = Number of cases + def T = reader.nextInt() + //Iteration through all the cases + 1.upto(T){ti-> + def X = reader.nextInt() //X = CJ Cost + def Y = reader.nextInt() //Y = JC Cost + def S = reader.nextLine() + S = S.trim() //S = String to analyse + //With dynamic programming + def cCost = S.charAt(0) == "J"? Double.POSITIVE_INFINITY: 0 + def jCost = S.charAt(0) == "C"? Double.POSITIVE_INFINITY: 0 + for(int i = 1; i < S.length(); i++){ + def cCostNew = Double.POSITIVE_INFINITY + def jCostNew = Double.POSITIVE_INFINITY + if(S.charAt(i) == "C"){ + cCostNew = Math.min(cCost, jCost + Y) + }else if(S.charAt(i) =="J"){ + jCostNew = Math.min(jCost, cCost + X) + }else{ + cCostNew = Math.min(cCost, jCost + Y) + jCostNew = Math.min(jCost, cCost + X) + } + cCost = cCostNew + jCost = jCostNew + } + //Print the final result for the case + println("Case #" + ti + ": " + (Math.min(cCost, jCost) as int) ) + } + } +} diff --git a/solutions/groovy/parenting_partnering_returns.groovy b/solutions/groovy/parenting_partnering_returns.groovy new file mode 100644 index 00000000..6e42b64d --- /dev/null +++ b/solutions/groovy/parenting_partnering_returns.groovy @@ -0,0 +1,49 @@ +//Problem to solve: Parenting Partnering Returns + +//Function: isOverLap +//Args: a: List of activities from Jamie or Cameron +// b: List of activities to do +def isOverlap(List a, List b){ + if(a.get(0) == -1) return false + if ((b.get(0) >= a.get(0) && b.get(0) < a.get(1)) || (b.get(1) >= a.get(0) && b.get(1) < a.get(1))) return true + return false +} + +def reader = new Scanner(System.in) //Define reader for the input +def T = reader.nextInt() //Read T = Number of cases +//Iteration through all the cases +1.upto(T) {ti-> + def N = reader.nextInt() //Read N = Number of activities + reader.nextLine() + def list = [] //Define list of activities to sort + def activities = [] //Define list of activities unsorted + for(int i = 0; i < N; i++) activities.add(reader.nextLine().trim()) //Read all the activities + for(int i = 0; i < N; i++){ //Pass the activities from String to Integer list + String[] h = activities.get(i).split("\\s") + def tempList = [] + for(int j = 0; j < h.length; j++) tempList.add(h[j] as int) + list.add(tempList) + } + Collections.sort(list, (a, b)->{ //Sort the activities by start time + return a.get(0) - b.get(0) + }); + def cameron = [-1,-1] //Define list of activities for Cameron + def jamie = [-1,-1] //Define list of activities for Jamie + String res = "" + def fin = false + for(int i = 0; i < list.size(); i++){ + if(!isOverlap(cameron, list.get(i))){ + cameron = list.get(i) + activities.set(activities.indexOf(list.get(i).get(0) + " " +list.get(i).get(1)), "C") //Replace with "C" in the unsorted list + }else if(!isOverlap(jamie, list.get(i))){ + jamie = list.get(i) + activities.set(activities.indexOf(list.get(i).get(0) + " " +list.get(i).get(1)), "J") //Replace with "J" in the unsorted list + }else{ + println("Case #" + ti + ": IMPOSSIBLE") + fin = true + break; + } + } + for(int i = 0; i < activities.size(); i++) res = res + activities.get(i) + if(!fin) println("Case #" + ti + ": " + res) //Print the answer for the case +} diff --git a/solutions/groovy/reversort_engineering.groovy b/solutions/groovy/reversort_engineering.groovy new file mode 100644 index 00000000..1feedefa --- /dev/null +++ b/solutions/groovy/reversort_engineering.groovy @@ -0,0 +1,83 @@ +//Problem to solve: Reversort Engineering +class Reversort { + static void main(String[] args) { + //Define reader for the input + def reader = new Scanner(System.in) + //T = Number of cases + def T = reader.nextInt() + //Iteration through all the cases + 1.upto(T) { ti -> + def N = reader.nextInt() //N = Size of the list + def C = reader.nextInt() //C = Desired cost + def minValue = N - 1; //Minimum cost of a list with size N: N-1 + def maxValue = ((N * (N+1))/2)-1; + ////Maximum cost of a list with size N: (N * (N - 1)) / 2 - 1 + if(C < minValue || C > maxValue){ + println("Case #"+ti+": IMPOSSIBLE") //IMPOSSIBLE case when C is not in range [N-1, (N * (N - 1)) / 2 - 1] + }else{ + def arr = [] //List + for(int i = 0; i < N; i++){ + arr.add(i+1) + } + + def maxArr = [] //List of size N with Max value of cost + for(int i = 0; i < N/2; i++){ + maxArr.putAt(i, (i+1)*2); + } + for(int i = N-1, m = 1; i > N/2 - 1; i--,m+=2){ + maxArr.putAt(i,m); + } + String sArr = ""; //Final List + switch(C) { + case minValue: //When C = N - 1 + for(int i = 0; i < arr.size(); i++){ + sArr = sArr + (i+1); + if(i < arr.size() - 1) sArr = sArr + " "; + } + break + case maxValue: //When C = (N * (N - 1)) / 2 - 1 + for(int i = 0; i < maxArr.size(); i++){ + sArr = sArr + maxArr.get(i); + if(i < maxArr.size() - 1) sArr = sArr + " "; + } + break + default: + //When C is in range (N-1, (N * (N - 1)) / 2 - 1) + int j, k = N - 1, cost = minValue + 1, f = 0; + def tArr = arr; + def fArr = []; + while(k>0){ + j = k -1; + while(j > f-1){ + fArr = []; + fArr.addAll(tArr.subList(0, f)); + fArr.addAll(tArr.subList(f, j)); + fArr.addAll(tArr.subList(j,k+1).reverse()); + if (k + 1 < tArr.size())fArr.addAll(tArr.subList(k+1, tArr.size())) + if(cost == C){ + for(int i = 0; i < fArr.size(); i++){ + sArr = sArr + fArr.get(i); + if(i < fArr.size() - 1) sArr = sArr + " "; + } + j = f -1; + k = 0; + } + cost++; + j--; + } + if(fArr.get(f) == maxArr.get(f)){ + f++; + } + if(fArr.get(k) == maxArr.get(k)){ + k--; + } + tArr = fArr; + } + } + //Print final answer for the case + println("Case #"+ti+": " + sArr) + } + } + } + +}