Source code for submission s1337

Ants.java

  1. import java.io.*;
  2.  
  3. public class Ants {
  4. public static void main(String[] args) throws Exception {
  5.  
  6. int length;
  7. int count;
  8.  
  9. while(true)
  10. {
  11. String line = in.readLine();
  12. if(line == null) break;
  13.  
  14. String data[] = new String[2];
  15.  
  16. data = line.split(" ");
  17.  
  18. length = new Integer(data[0]);
  19. count = new Integer(data[1]);
  20.  
  21. int pos, posmax1 = -1;
  22. int posmax2 = -1;
  23. int d, d1, d2;
  24. int maxd = 0;
  25. int TYP = -2; // -2 = zadny, -1 = R, 1 = L, 0 = L i R
  26.  
  27. String ant[] = new String[2];
  28.  
  29. for(int i = 0; i < count; i++)
  30. {
  31. line = in.readLine();
  32.  
  33. ant = line.split(" ");
  34. pos = new Integer(ant[0]);
  35.  
  36. if(ant[1].equals("R"))
  37. {
  38. d = length-pos;
  39. if(TYP == -1 || TYP == 0){
  40. if(d > maxd){
  41. maxd = d;
  42. posmax1 = pos;
  43. posmax2 = -1;
  44. TYP = -1;
  45. }
  46. }else{
  47. if(d > maxd){
  48. maxd = d;
  49. posmax1 = pos;
  50. posmax2 = -1;
  51. TYP = -1;
  52. }
  53. if(d == maxd){
  54. posmax1 = pos;
  55. TYP = 0;
  56. }
  57. }
  58.  
  59. }
  60. else
  61. {
  62. d = pos;
  63. if(TYP == 1 || TYP == 0){
  64. if(d > maxd){
  65. maxd = d;
  66. posmax2 = pos;
  67. posmax1 = -1;
  68. TYP = 1;
  69. }
  70. }else{
  71. if(d > maxd){
  72. maxd = d;
  73. posmax2 = pos;
  74. posmax1 = -1;
  75. TYP = 1;
  76. }
  77. if(d == maxd){
  78. posmax2 = pos;
  79. TYP = 0;
  80. }
  81. }
  82. }
  83.  
  84.  
  85. }
  86.  
  87. if(posmax1 == posmax2) posmax1 = -1;
  88.  
  89. if(posmax1 == -1) System.out.println("The last ant will fall down in "+maxd+" seconds - started at "+posmax2+".");
  90. else if(posmax2 == -1) System.out.println("The last ant will fall down in "+maxd+" seconds - started at "+posmax1+".");
  91. else{
  92. int a,b;
  93. if (posmax1 > posmax2){
  94. a = posmax2;
  95. b = posmax1;
  96. }
  97. else{
  98. a = posmax1;
  99. b = posmax2;
  100. }
  101. System.out.println("The last ant will fall down in "+maxd+" seconds - started at "+a+" and "+b+".");
  102. }
  103. }
  104. }
  105. }
  106.