Go to diff to previous submission
import java.util.Scanner; import java.util.Arrays; import java.util.TreeMap; public class Ants { static int length; static int count; static int[] position; static int left = -1; static int right = -1; position = new int[100000]; while (sc.hasNextInt()) { length = sc.nextInt(); count = sc.nextInt(); int[] indexes = new int[count]; for (int i = 0; i < count; i++) { position[i] = sc.nextInt(); direction[i] = sc.next(); indexes[i] = i; } sort(indexes, position); //for (int i = 0; i < count; i++) { // System.out.print(indexes[i] + ", "); //} //System.out.println(position[0] + " " + direction[0]); left = -1; right = -1; int max = 0; for (int i = 0; i < count; i++) { if(direction[indexes[i]].equals("L")){ if(position[indexes[i]] >= max){ max = position[indexes[i]]; left = i; //System.out.println("left " + left + " " + position[left]); } }else{ if((length - position[indexes[i]]) >= max){ max = length - position[indexes[i]]; right = i; //System.out.println("right " + right + " " + position[right]); } } } int antsR = 0; int antsL = 0; if(right != -1){ for (int i = right; i < count; i++) { //System.out.println("111 " + direction[indexes[i]]); if(direction[indexes[i]].equals("L")){ antsR++; } } } if(left != -1){ for (int i = left; i >= 0; i--) { //System.out.println("222 " + direction[indexes[i]]); if(direction[indexes[i]].equals("R")){ antsL++; } } } //System.out.println(antsL + " " + antsR); // ******************** int resultA = -1; if(right != -1){ if((length - position[indexes[right]]) == max){ resultA = position[indexes[right + antsR]]; } } int resultB = -1; if(left != -1){ if((position[indexes[left]]) == max){ resultB = position[indexes[left - antsL]]; } } if((resultA >= 0) && (resultB == -1)){ System.out.println("The last ant will fall down in " + max + " seconds - started at " + resultA + "."); } if((resultA == -1) && (resultB >= 0)){ System.out.println("The last ant will fall down in " + max + " seconds - started at " + resultB + "."); } if((resultA >= 0) && (resultB >= 0)){ if (resultA > resultB) { int nic = resultA; resultA = resultB; resultB = nic; } + " and " + resultB + "."); } // System.out.println("The last ant will fall down in " + max + " seconds - started at " + xxx); } } public static void sort(int[] indexes, int[] position) { /* for (int i = 0; i < count - 1; i++) { for (int j = i + 1; j < count; j++) { if (position[i] > position[j]) { int a = position[i]; position[i] = position[j]; position[j] = a; a = indexes[i]; indexes[i] = indexes[j]; indexes[j] = a; } } } */ for (int i = 0; i < count; i++) { map.put(position[i], i); } int k = 0; indexes[k] = p; k++; } //for (int i = 0; i < count; i++) { //indexes[i] = map.get(position[i]); // System.out.println(i + " " + position[indexes[i]] + " " + indexes[i]); //} } }
--- c4.s974.cteam094.ants.java.0.Ants.java +++ c4.s1072.cteam094.ants.java.0.Ants.java @@ -1,4 +1,5 @@ import java.util.Scanner; import java.util.Arrays; +import java.util.TreeMap; public class Ants { @@ -43,12 +44,16 @@ for (int i = 0; i < count; i++) { if(direction[indexes[i]].equals("L")){ - if(position[i] >= max){ - max = position[i]; - left = i; + if(position[indexes[i]] >= max){ + max = position[indexes[i]]; + left = i; + //System.out.println("left " + left + " " + position[left]); + } }else{ - if((length - position[i]) >= max){ - max = length - position[i]; - right = i; + if((length - position[indexes[i]]) >= max){ + max = length - position[indexes[i]]; + right = i; + //System.out.println("right " + right + " " + position[right]); + } } @@ -75,12 +80,12 @@ int resultA = -1; if(right != -1){ - if((length - position[right]) == max){ - resultA = position[right + antsR]; + if((length - position[indexes[right]]) == max){ + resultA = position[indexes[right + antsR]]; } } int resultB = -1; if(left != -1){ - if((position[left]) == max){ - resultB = position[left - antsL]; + if((position[indexes[left]]) == max){ + resultB = position[indexes[left - antsL]]; } } @@ -106,4 +111,5 @@ public static void sort(int[] indexes, int[] position) { + /* for (int i = 0; i < count - 1; i++) { for (int j = i + 1; j < count; j++) { @@ -120,4 +126,19 @@ } + */ + TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>(); + for (int i = 0; i < count; i++) { + map.put(position[i], i); + } + int k = 0; + for (Integer p : map.values()) { + indexes[k] = p; + k++; + } + + //for (int i = 0; i < count; i++) { + //indexes[i] = map.get(position[i]); + // System.out.println(i + " " + position[indexes[i]] + " " + indexes[i]); + //} } }