Source code for submission s974

Ants.java

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