Source code for submission s1072

Go to diff to previous submission

Ants.java

  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3. import java.util.TreeMap;
  4.  
  5. public class Ants {
  6.  
  7. static int length;
  8. static int count;
  9.  
  10. static int[] position;
  11. static String[] direction;
  12.  
  13. static int left = -1;
  14. static int right = -1;
  15.  
  16.  
  17. public static void main(String[] args) throws Exception {
  18. position = new int[100000];
  19. direction = new String[100000];
  20. Scanner sc = new Scanner(System.in);
  21. while (sc.hasNextInt()) {
  22. length = sc.nextInt();
  23. count = sc.nextInt();
  24. int[] indexes = new int[count];
  25. for (int i = 0; i < count; i++) {
  26. position[i] = sc.nextInt();
  27. direction[i] = sc.next();
  28. indexes[i] = i;
  29. }
  30.  
  31. sort(indexes, position);
  32.  
  33. //for (int i = 0; i < count; i++) {
  34. // System.out.print(indexes[i] + ", ");
  35. //}
  36.  
  37.  
  38.  
  39.  
  40. //System.out.println(position[0] + " " + direction[0]);
  41. left = -1;
  42. right = -1;
  43. int max = 0;
  44. for (int i = 0; i < count; i++) {
  45. if(direction[indexes[i]].equals("L")){
  46. if(position[indexes[i]] >= max){
  47. max = position[indexes[i]];
  48. left = i;
  49. //System.out.println("left " + left + " " + position[left]);
  50.  
  51. }
  52. }else{
  53. if((length - position[indexes[i]]) >= max){
  54. max = length - position[indexes[i]];
  55. right = i;
  56. //System.out.println("right " + right + " " + position[right]);
  57.  
  58. }
  59. }
  60. }
  61. int antsR = 0;
  62. int antsL = 0;
  63. if(right != -1){
  64. for (int i = right; i < count; i++) {
  65. //System.out.println("111 " + direction[indexes[i]]);
  66. if(direction[indexes[i]].equals("L")){
  67. antsR++;
  68. }
  69. }
  70. }
  71. if(left != -1){
  72. for (int i = left; i >= 0; i--) {
  73. //System.out.println("222 " + direction[indexes[i]]);
  74. if(direction[indexes[i]].equals("R")){
  75. antsL++;
  76. }
  77. }
  78. }
  79. //System.out.println(antsL + " " + antsR); // ********************
  80. int resultA = -1;
  81. if(right != -1){
  82. if((length - position[indexes[right]]) == max){
  83. resultA = position[indexes[right + antsR]];
  84. }
  85. }
  86. int resultB = -1;
  87. if(left != -1){
  88. if((position[indexes[left]]) == max){
  89. resultB = position[indexes[left - antsL]];
  90. }
  91. }
  92. if((resultA >= 0) && (resultB == -1)){
  93. System.out.println("The last ant will fall down in " + max + " seconds - started at " + resultA + ".");
  94. }
  95. if((resultA == -1) && (resultB >= 0)){
  96. System.out.println("The last ant will fall down in " + max + " seconds - started at " + resultB + ".");
  97. }
  98. if((resultA >= 0) && (resultB >= 0)){
  99.  
  100. if (resultA > resultB) {
  101. int nic = resultA;
  102. resultA = resultB;
  103. resultB = nic;
  104. }
  105. System.out.println("The last ant will fall down in " + max + " seconds - started at " + resultA
  106. + " and " + resultB + ".");
  107. }
  108. // System.out.println("The last ant will fall down in " + max + " seconds - started at " + xxx);
  109. }
  110. }
  111.  
  112. public static void sort(int[] indexes, int[] position) {
  113. /*
  114. for (int i = 0; i < count - 1; i++) {
  115. for (int j = i + 1; j < count; j++) {
  116. if (position[i] > position[j]) {
  117. int a = position[i];
  118. position[i] = position[j];
  119. position[j] = a;
  120.  
  121.   a = indexes[i];
  122. indexes[i] = indexes[j];
  123. indexes[j] = a;
  124. }
  125. }
  126. }
  127.  
  128. */
  129. TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
  130. for (int i = 0; i < count; i++) {
  131. map.put(position[i], i);
  132. }
  133. int k = 0;
  134. for (Integer p : map.values()) {
  135. indexes[k] = p;
  136. k++;
  137. }
  138.  
  139. //for (int i = 0; i < count; i++) {
  140. //indexes[i] = map.get(position[i]);
  141. // System.out.println(i + " " + position[indexes[i]] + " " + indexes[i]);
  142. //}
  143. }
  144. }
  145.  
  146.  
  147.  
  148.  

Diff to submission s974

Ants.java

--- 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]);
+                //}
         }
 }