Go to diff to previous submission
import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; /** * * @author cteam049 */ public class ants { protected Ant[] ants; protected int antsCnt; protected int leftCnt; protected int rightCnt; /** * @param args the command line arguments */ ants program = new ants(); program.run(); } protected void run() { try { for (;;) { int l = this.nextInt(); 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++) { pos = this.nextInt(); direction = this.nextToken().charAt(0); this.ants[i] = new Ant(pos, direction); if ( direction == 'L' ) { this.leftCnt++; } else { this.rightCnt++; } } int maxL = -10; int maxR = -10; int posL = 0; int posR = 0; for ( int i = this.antsCnt - 1; i >= 0; i-- ) { if ( this.ants[ i ].direction == 'L' ) { maxL = this.ants[ i ].pos; posL = i; break; } } for ( int i = 0; i < this.antsCnt; i++ ) { if ( this.ants[ i ].direction == 'R' ) { maxR = l - this.ants[ i ].pos; posR = i; break; } } if ( maxL == -10 ) { System.out.println( "The last ant will fall down in " + maxR + " seconds - started at " + this.ants[ 0 ].pos + "." ); continue; } if ( maxR == -10 ) { System.out.println( "The last ant will fall down in " + maxL + " seconds - started at " + this.ants[ this.antsCnt - 1 ].pos + "." ); continue; } if ( maxR == maxL ) { 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 ) { 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 { 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 tmp; do { swapped = false; for ( int i = 0; i < this.antsCnt - 1; i++ ) { if ( this.ants[ i ].direction == 'R' && this.ants[ i + 1 ].direction == 'L' ) { this.ants[ i ].direction = 'L'; this.ants[ i + 1 ].direction = 'R'; tmp = this.ants[ i ].last; this.ants[ i ].last = this.ants[ i + 1 ].last; this.ants[ i + 1 ].last = tmp; swapped = true; } } } while ( swapped );*/ /*boolean two = false; for ( int i = 0; i < this.antsCnt; i++ ) { if ( this.ants[ i ].last ) { if ( ! two ) { two = true; System.out.print( "The last ant will fall down in " + Math.max( maxR, maxL ) + " seconds - started at " + this.ants[ i ].pos ); } else { System.out.print( " and " + this.ants[ i ].pos ); } } } System.out.println(".");*/ } e.printStackTrace(); } } protected class Ant implements Comparable<Ant> { public int pos; public char direction; public boolean last; public Ant(int pos, char direction) { this.pos = pos; this.direction = direction; this.last = false; } public int compareTo( Ant ant ) { return this.pos - ant.pos; } } /* INPUT READING */ while (!st.hasMoreTokens()) { } return st.nextToken(); } } return input.readLine(); } }
--- 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(".");*/ }