Source code for submission s1231

Ants.java

  1. package javaapplication1;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.List;
  9.  
  10. public class Ants {
  11.  
  12. public static void main(String[] args) throws IOException {
  13. String line;
  14. while ((line = reader.readLine()) != null) {
  15. String[] split = line.split(" ");
  16.  
  17. int len = Integer.parseInt(split[0]);
  18. int n = Integer.parseInt(split[1]);
  19.  
  20. List<Integer> arr = new ArrayList<Integer>();
  21.  
  22. int max = -1;
  23. int min = len + 1;
  24. int lCount = 0;
  25.  
  26. for (int i = 0; i < n; i++) {
  27. line = reader.readLine();
  28. split = line.split(" ");
  29.  
  30. int pos = Integer.parseInt(split[0]);
  31.  
  32. if (split[1].equals("R")) {
  33. if (pos < min) {
  34. min = pos;
  35. }
  36. } else {
  37. if (pos > max) {
  38. max = pos;
  39. }
  40. lCount++;
  41. }
  42. arr.add(pos);
  43. }
  44.  
  45. Collections.sort(arr);
  46.  
  47. if(max == -1) lCount++;
  48. if(min == len+1) lCount--;
  49.  
  50. if (min < len - max) {
  51. System.out.println("The last ant will fall down in " + (len - min)
  52. + " seconds - started at " + arr.get(lCount - 1) + ".");
  53. } else if (min > len - max) {
  54. System.out.println("The last ant will fall down in " + max
  55. + " seconds - started at " + arr.get(lCount) + ".");
  56. } else {
  57. System.out.println("The last ant will fall down in " + max
  58. + " seconds - started at " + arr.get(lCount-1)
  59. + " and "+ arr.get(lCount) + ".");
  60. }
  61. }
  62. reader.readLine();
  63. }
  64. }
  65.