Source code for submission s1032

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. do
  69. {
  70. ordered = true;
  71. for(int i=1;i<mrav;i++)
  72. {
  73. if(!ants.get(i-1).goesleft && ants.get(i).goesleft)
  74. {
  75. ordered = false;
  76. ants.get(i-1).goesleft = true;
  77. ants.get(i).goesleft = false;
  78. }
  79. }
  80. }while(!ordered);
  81. int boundary = 0;
  82. for(int i=0;i<mrav;i++)
  83. {
  84. if(!ants.get(i).goesleft)
  85. {
  86. boundary = i;
  87. break;
  88. }
  89. boundary++;
  90. }
  91. System.out.print("The last ant will fall down in "+maxdist+" seconds - started at ");
  92. if(pp.size()==1)
  93. System.out.print(ants.get(maxright?boundary:(boundary-1)).apos);
  94. else
  95. System.out.print(ants.get(boundary-1).apos+" and "+ants.get(boundary).apos);
  96. System.out.println(".");
  97. }
  98. }
  99.  
  100. public static class Ant
  101. {
  102. int apos;
  103. boolean goesleft;
  104. int fallsin=-1;
  105.  
  106. public Ant(int apos, boolean goesleft,int fallsin) {
  107. this.apos = apos;
  108. this.goesleft = goesleft;
  109. this.fallsin = fallsin;
  110. }
  111. public String toString()
  112. {
  113. return apos+"-"+goesleft;
  114. }
  115.  
  116. }
  117. }