Source code for submission s1057

ants.java

  1.  
  2. import java.util.StringTokenizer;
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.util.Arrays;
  6.  
  7. /**
  8.  *
  9.  * @author cteam049
  10.  */
  11. public class ants {
  12.  
  13. protected Ant[] ants;
  14. protected int antsCnt;
  15.  
  16. /**
  17.   * @param args the command line arguments
  18.   */
  19. public static void main(String[] args) {
  20. ants program = new ants();
  21. program.run();
  22. }
  23.  
  24. protected void run() {
  25. try {
  26. for (;;) {
  27. int l = this.nextInt();
  28. this.antsCnt = this.nextInt();
  29. this.ants = new Ant[ this.antsCnt ];
  30.  
  31. for (int i = 0; i < this.antsCnt; i++) {
  32. this.ants[i] = new Ant(this.nextInt(), this.nextToken().charAt(0));
  33. }
  34.  
  35. Arrays.sort( this.ants );
  36.  
  37.  
  38. int maxL = -10;
  39. int maxR = -10;
  40. int posL = 0;
  41. int posR = 0;
  42.  
  43. for ( int i = this.antsCnt - 1; i >= 0; i-- )
  44. {
  45. if ( this.ants[ i ].direction == 'L' )
  46. {
  47. maxL = this.ants[ i ].pos;
  48. posL = i;
  49. break;
  50. }
  51. }
  52.  
  53. for ( int i = 0; i < this.antsCnt; i++ )
  54. {
  55. if ( this.ants[ i ].direction == 'R' )
  56. {
  57. maxR = l - this.ants[ i ].pos;
  58. posR = i;
  59. break;
  60. }
  61. }
  62.  
  63.  
  64. if ( maxL == -10 )
  65. {
  66. System.out.println( "The last ant will fall down in " + maxR + " seconds - started at " + this.ants[ 0 ].pos + "." );
  67. continue;
  68. }
  69.  
  70. if ( maxR == -10 )
  71. {
  72. System.out.println( "The last ant will fall down in " + maxL + " seconds - started at " + this.ants[ this.antsCnt - 1 ].pos + "." );
  73. continue;
  74. }
  75.  
  76.  
  77. if ( maxR == maxL )
  78. {
  79. this.ants[ posL ].last = true;
  80. this.ants[ posR ].last = true;
  81. }
  82. else if ( maxR < maxL )
  83. {
  84. this.ants[ posL ].last = true;
  85. }
  86. else
  87. {
  88. this.ants[ posR ].last = true;
  89. }
  90.  
  91.  
  92. boolean swapped;
  93. boolean tmp;
  94.  
  95. do
  96. {
  97. swapped = false;
  98. for ( int i = 0; i < this.antsCnt - 1; i++ )
  99. {
  100. if ( this.ants[ i ].direction == 'R' && this.ants[ i + 1 ].direction == 'L' )
  101. {
  102. this.ants[ i ].direction = 'L';
  103. this.ants[ i + 1 ].direction = 'R';
  104.  
  105. tmp = this.ants[ i ].last;
  106. this.ants[ i ].last = this.ants[ i + 1 ].last;
  107. this.ants[ i + 1 ].last = tmp;
  108.  
  109. swapped = true;
  110. }
  111. }
  112. } while ( swapped );
  113.  
  114.  
  115. boolean two = false;
  116. for ( int i = 0; i < this.antsCnt; i++ )
  117. {
  118. if ( this.ants[ i ].last )
  119. {
  120. if ( ! two )
  121. {
  122. two = true;
  123. System.out.print( "The last ant will fall down in " + Math.max( maxR, maxL ) + " seconds - started at " + this.ants[ i ].pos );
  124. }
  125. else
  126. {
  127. System.out.print( " and " + this.ants[ i ].pos );
  128. }
  129. }
  130. }
  131. System.out.println(".");
  132.  
  133. }
  134. } catch (Exception e) {
  135. e.printStackTrace();
  136. }
  137. }
  138.  
  139.  
  140.  
  141. protected class Ant implements Comparable<Ant> {
  142.  
  143. public int pos;
  144. public char direction;
  145. public boolean last;
  146.  
  147. public Ant(int pos, char direction) {
  148. this.pos = pos;
  149. this.direction = direction;
  150. this.last = false;
  151. }
  152.  
  153. public int compareTo( Ant ant ) {
  154. return this.pos - ant.pos;
  155. }
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165. /* INPUT READING */
  166.  
  167. public String nextToken() throws Exception {
  168. while (!st.hasMoreTokens()) {
  169. st = new StringTokenizer(input.readLine());
  170. }
  171. return st.nextToken();
  172. }
  173.  
  174. public int nextInt() throws Exception {
  175. return Integer.parseInt(this.nextToken());
  176. }
  177.  
  178. public String nextLine() throws Exception {
  179. return input.readLine();
  180. }
  181. }
  182.