Source code for submission s1186

Ants.java

  1.  
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.Comparator;
  8. import java.util.StringTokenizer;
  9.  
  10. public class Ants {
  11.  
  12. static ArrayList<Ant> ants = new ArrayList<Ant>();
  13.  
  14. static class Ant {
  15.  
  16. boolean d;
  17. int p, t = 0, s;
  18. }
  19. static Comparator<Ant> antComp = new Comparator<Ant>() {
  20.  
  21. public int compare(Ant a1, Ant a2) {
  22. return (a1.s - a2.s);
  23. }
  24. };
  25.  
  26. public static void main(String[] args) throws IOException {
  27.  
  28. int L, A, s, e, x;
  29.  
  30. do {
  31. String line = br.readLine();
  32. if (line == null) {
  33. break;
  34. }
  35. L = 2 * Integer.parseInt(st.nextToken());
  36. A = Integer.parseInt(st.nextToken());
  37. ants.clear();
  38. for (int i = 0; i < A; i++) {
  39. st = new StringTokenizer(br.readLine());
  40. Ant a = new Ant();
  41. a.s = a.p = 2 * Integer.parseInt(st.nextToken());
  42. a.d = st.nextToken().equals("R") ? true : false;
  43. ants.add(a);
  44. }
  45. s = 0;
  46. e = A - 1;
  47. Collections.sort(ants, antComp);
  48. while (s <= e) {
  49. if (!ants.get(s).d) {
  50. ants.get(s).t += ants.get(s).p;
  51. ants.get(s).p = 0;
  52. s++;
  53. } else if (ants.get(e).d) {
  54. ants.get(e).t += L - ants.get(e).p;
  55. ants.get(e).p = L;
  56. e--;
  57. } else {
  58. for (int i = s; i < e; i++) {
  59. if (ants.get(i).d && !ants.get(i + 1).d) {
  60. if (ants.get(i).t > ants.get(i + 1).t) {
  61. ants.get(i + 1).p -= ants.get(i).t - ants.get(i + 1).t;
  62. ants.get(i + 1).t = ants.get(i).t;
  63. } else if (ants.get(i).t < ants.get(i + 1).t) {
  64. ants.get(i).p += ants.get(i + 1).t - ants.get(i).t;
  65. ants.get(i).t = ants.get(i + 1).t;
  66. }
  67. x = (ants.get(i + 1).p - ants.get(i).p) / 2;
  68. ants.get(i).t += x;
  69. ants.get(i + 1).t += x;
  70. ants.get(i).p += x;
  71. ants.get(i + 1).p -= x;
  72. ants.get(i).d = false;
  73. ants.get(i + 1).d = true;
  74. }
  75. }
  76. }
  77. }
  78. System.out.print("The last ant will fall down in ");
  79. if (s < A && e >= 0) {
  80. if (ants.get(s).t == ants.get(e).t) {
  81. System.out.print(ants.get(s).t / 2 + " seconds - started at " + ants.get(e).s / 2 + " and " + ants.get(s).s / 2 + ".");
  82. } else {
  83. if (ants.get(s).t > ants.get(e).t) {
  84. System.out.print(ants.get(s).t / 2 + " seconds - started at " + ants.get(s).s / 2 + ".");
  85. } else {
  86. System.out.print(ants.get(e).t / 2 + " seconds - started at " + ants.get(e).s / 2 + ".");
  87. }
  88. }
  89. } else if (s < A) {
  90. System.out.print(ants.get(s).t / 2 + " seconds - started at " + ants.get(s).s / 2 + ".");
  91. } else {
  92. System.out.print(ants.get(e).t / 2 + " seconds - started at " + ants.get(e).s / 2 + ".");
  93. }
  94.  
  95. } while (true);
  96.  
  97. }
  98. }
  99.