Source code for submission s1127

Go to diff to previous submission

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

Diff to submission s1057

ants.java

--- c4.s1057.cteam049.ants.java.0.ants.java
+++ c4.s1127.cteam049.ants.java.0.ants.java
@@ -9,9 +9,14 @@
  * @author cteam049
  */
-public class ants {
-
+public class ants
+{
+    
     protected Ant[] ants;
-    protected int antsCnt;
+    protected int   antsCnt;
+    protected int   leftCnt;
+    protected int   rightCnt;
 
+    
+    
     /**
      * @param args the command line arguments
@@ -28,7 +33,24 @@
                 this.antsCnt = this.nextInt();
                 this.ants = new Ant[ this.antsCnt ];
+                this.leftCnt = 0;
+                this.rightCnt = 0;
+                
+                int pos;
+                char direction;
 
                 for (int i = 0; i < this.antsCnt; i++) {
-                    this.ants[i] = new Ant(this.nextInt(), this.nextToken().charAt(0));
+                    
+                    pos = this.nextInt();
+                    direction = this.nextToken().charAt(0);
+                    this.ants[i] = new Ant(pos, direction);
+                    
+                    if ( direction == 'L' )
+                    {
+                        this.leftCnt++;
+                    }
+                    else
+                    {
+                        this.rightCnt++;
+                    }
                 }
 
@@ -77,18 +99,21 @@
                 if ( maxR == maxL )
                 {
-                    this.ants[ posL ].last = true;
-                    this.ants[ posR ].last = true;
+                    System.out.println( "The last ant will fall down in " + maxL + " seconds - started at " + this.ants[ this.leftCnt - 1 ].pos + " and " + this.ants[ this.antsCnt - this.rightCnt ].pos + "." );
+                    // this.ants[ posL ].last = true;
+                    // this.ants[ posR ].last = true;
                 }
                 else if ( maxR < maxL )
                 {
-                    this.ants[ posL ].last = true;
+                    System.out.println( "The last ant will fall down in " + maxL + " seconds - started at " + this.ants[ this.leftCnt - 1 ].pos + "." );
+                    // this.ants[ posL ].last = true;
                 }
                 else
                 {
-                    this.ants[ posR ].last = true;
+                    System.out.println( "The last ant will fall down in " + maxR + " seconds - started at " + this.ants[ this.antsCnt - this.rightCnt ].pos + "." );
+                    // this.ants[ posR ].last = true;
                 }
                 
 
-                boolean swapped;
+                /*boolean swapped;
                 boolean tmp;
                 
@@ -110,8 +135,8 @@
                         }
                     }
-                } while ( swapped );
+                } while ( swapped );*/
                 
                 
-                boolean two = false;
+                /*boolean two = false;
                 for ( int i = 0; i < this.antsCnt; i++ )
                 {
@@ -129,5 +154,5 @@
                     }
                 }
-                System.out.println(".");
+                System.out.println(".");*/
                 
             }