Source code for submission s1367

Go to diff to previous submission

ants.java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /**
  7.  *
  8.  * @author cteam040
  9.  */
  10. import java.util.*;
  11. import java.io.*;
  12.  
  13. public class ants
  14. {
  15. public static void main(String[] s) throws Exception
  16. {
  17. String ln;
  18. while((ln=br.readLine())!=null)
  19. {
  20. st = new StringTokenizer(ln);
  21. boolean maxright=false,maxleft = true;
  22.  
  23. List<Integer> pp = new ArrayList<Integer>(2);
  24. final int len = Integer.parseInt(st.nextToken());
  25. int mrav = Integer.parseInt(st.nextToken());
  26. List<Ant> ants = new ArrayList<Ant>(mrav);
  27. int maxdist = -1;
  28. for(int i=0;i<mrav;i++)
  29. {
  30. st = new StringTokenizer(br.readLine());
  31. int apos = Integer.parseInt(st.nextToken());
  32. int dist = -1;
  33. boolean left = false;
  34. if("R".equals(st.nextToken()))
  35. {
  36. dist = len-apos;
  37. left = false;
  38. ants.add(new Ant(apos,false,dist));
  39. }
  40. else
  41. {
  42. dist = apos;
  43. left = true;
  44. ants.add(new Ant(apos,true,dist));
  45. }
  46. if(dist>maxdist)
  47. {
  48. pp.clear();
  49. maxleft = false;
  50. maxright = false;
  51. maxdist = dist;
  52. }
  53. if(dist==maxdist)
  54. {
  55. if(left) maxleft = true;
  56. else maxright = true;
  57. pp.add(apos);
  58. }
  59. }
  60. Collections.sort(ants, new Comparator<Ant>(){
  61.  
  62. public int compare(Ant arg0, Ant arg1) {
  63. return arg0.apos-arg1.apos;
  64. }
  65. });
  66.  
  67. boolean ordered = true;
  68. int itcnt = 0;
  69. do
  70. {
  71. ordered = true;
  72.  
  73. for(int i=0;i<mrav-1;i++)
  74. {
  75. if(ants.get(i).goesleft)
  76. continue;
  77. // assert goes right*/
  78. for(int j=i+1;j<mrav;j++)
  79. {
  80. itcnt ++;
  81. if(!ants.get(j-1).goesleft && ants.get(j).goesleft)
  82. {
  83. ordered = false;
  84. ants.get(j-1).goesleft = true;
  85. ants.get(j).goesleft = false;
  86. }
  87. }
  88. }
  89. }while(!ordered);
  90. // System.out.println(itcnt);
  91. int boundary = 0;
  92. for(int i=0;i<mrav;i++)
  93. {
  94. if(!ants.get(i).goesleft)
  95. {
  96. boundary = i;
  97. break;
  98. }
  99. boundary++;
  100. }
  101. System.out.print("The last ant will fall down in "+maxdist+" seconds - started at ");
  102. if(pp.size()==1)
  103. System.out.print(ants.get(maxright?boundary:(boundary-1)).apos);
  104. else
  105. System.out.print(ants.get(boundary-1).apos+" and "+ants.get(boundary).apos);
  106. System.out.println(".");
  107. }
  108. }
  109.  
  110.  
  111.  
  112. public static class Ant
  113. {
  114. int apos;
  115. boolean goesleft;
  116. int fallsin=-1;
  117.  
  118. public Ant(int apos, boolean goesleft,int fallsin) {
  119. this.apos = apos;
  120. this.goesleft = goesleft;
  121. this.fallsin = fallsin;
  122. }
  123. public String toString()
  124. {
  125. return apos+"-"+goesleft;
  126. }
  127.  
  128. }
  129. }

Diff to submission s1111

ants.java

--- c4.s1111.cteam040.ants.java.0.ants.java
+++ c4.s1367.cteam040.ants.java.0.ants.java
@@ -111,4 +111,5 @@
         
         
+        
                 public static class Ant
                 {