Source code for submission s1027

Main.java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package ants;
  6.  
  7. import java.util.PriorityQueue;
  8.  
  9. /**
  10.  *
  11.  * @author cteam066
  12.  */
  13. public class Main {
  14.  
  15. /**
  16.   * @param args the command line arguments
  17.   */
  18. public static void main(String[] args) {
  19. java.util.Scanner in = new java.util.Scanner(System.in);
  20. int intovepole[] = new int[100001];
  21. int prevodnypole[] = new int[100001];
  22. char charovypole[] = new char[100001];
  23.  
  24. while (in.hasNextInt()) {
  25. int L = in.nextInt();
  26. int A = in.nextInt();
  27. int maxR = -1;
  28. int maxL = -1;
  29. PriorityQueue<data> fronta = new PriorityQueue<data>();
  30.  
  31. for (int i = 0; i <= L; ++i) {
  32. prevodnypole[i] = i;
  33. }
  34.  
  35. for (int i = 0; i < A; ++i) {
  36. int a = in.nextInt();
  37. char c = in.next().charAt(0);
  38. intovepole[i] = a;
  39. charovypole[a] = c;
  40. }
  41. for (int i = 0; i < A; ++i) {
  42. if (charovypole[intovepole[i]] == 'R') {
  43. if (L - intovepole[i] > maxR) {
  44. maxR = L - intovepole[i];
  45. }
  46.  
  47. for (int j = 0; j < A; ++j) {
  48. if (charovypole[intovepole[j]] == 'L' &&
  49. intovepole[j] > intovepole[i]) {
  50. fronta.add(new data(intovepole[i], intovepole[j], intovepole[j] - intovepole[i]));
  51. }
  52.  
  53. }
  54.  
  55. } else if (intovepole[i] > maxL) {
  56. maxL = intovepole[i];
  57. }
  58. }
  59.  
  60.  
  61.  
  62. while (!fronta.isEmpty()) {
  63. data d = fronta.poll();
  64. int temp = prevodnypole[d.i];
  65. prevodnypole[d.i] = prevodnypole[d.j];
  66. prevodnypole[d.j] = temp;
  67. }
  68.  
  69. System.out.print("The last ant will fall down in " + (maxR > maxL ? maxR : maxL) + " seconds - started at ");
  70.  
  71. if (maxR > maxL) {
  72. System.out.println(prevodnypole[L - maxR] + ".");
  73. } else if (maxL > maxR) {
  74. System.out.println(prevodnypole[maxL] + ".");
  75. } else {
  76. int min = prevodnypole[L - maxR] < prevodnypole[maxL] ? prevodnypole[L - maxR] : prevodnypole[maxL];
  77. int max = prevodnypole[L - maxR] + prevodnypole[maxL] - min;
  78.  
  79. System.out.println(min + " and " + max + ".");
  80. }
  81. }
  82. }
  83.  
  84. static class data implements Comparable<data> {
  85.  
  86. data(int _i, int _j, int _k) {
  87. i = _i;
  88. j = _j;
  89. key = _k;
  90. }
  91.  
  92. public int compareTo(data arg0) {
  93. return key - arg0.key;
  94.  
  95. }
  96. public int i, j, key;
  97. }
  98. }
  99.