Source code for submission s588

Ants.java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package acm;
  7.  
  8. import java.util.Scanner;
  9.  
  10. /**
  11.  *
  12.  * @author cteam030
  13.  */
  14. public class Ants {
  15.  
  16. public static void main(String[] args) {
  17.  
  18. Scanner s = new Scanner(System.in);
  19.  
  20. while(s.hasNext())
  21. {
  22. int d = s.nextInt();
  23. int count = s.nextInt();
  24. int max = 0;
  25. int middle1 = -1;
  26. int middle2 = -1;
  27.  
  28. for(int i = 0; i < count; i++)
  29. {
  30. int pos = s.nextInt();
  31. String dir = s.next();
  32.  
  33. int len = dir.equals("R") ? d - pos : pos;
  34.  
  35. if(len > max)
  36. {
  37. max = len;
  38. }
  39.  
  40. double delta = Math.abs(pos - d/2);
  41. if(delta < Math.abs(middle2 - d/2))
  42. {
  43. if(delta < Math.abs(middle1 - d/2))
  44. {
  45. middle2 = middle1;
  46. middle1 = pos;
  47. }else
  48. {
  49. middle2 = pos;
  50. }
  51. }
  52. }
  53.  
  54. System.out.printf("The last ant will fall down in %d seconds - started at %d", max, middle1);
  55.  
  56. if(middle2 >= 0)
  57. {
  58. System.out.printf(" and %d", middle2);
  59. }
  60.  
  61. System.out.printf(".%n");
  62.  
  63. }
  64.  
  65. }
  66.  
  67. }
  68.