Source code for submission s1224

Go to diff to previous submission

ants.c

  1. #include <stdio.h>
  2.  
  3. #define LEFT 'L'
  4. #define RIGHT 'R'
  5. #define NONE '\0'
  6.  
  7. int falltime(int position, char direction, int length) {
  8. if (direction == 'R') {
  9. return length - position;
  10. }
  11.  
  12. return position;
  13. }
  14.  
  15. char directions[100000];
  16.  
  17. int main(void) {
  18. int length, count, position, time, max_time, i, l, r, last_left_going_left, last_left_going_right, last_right_going_left, last_right_going_right, max, max2, pos, pos2;
  19. int posibilities[4], posibilities_directions[4];
  20. char direction;
  21.  
  22. while (scanf("%d %d", &length, &count) == 2) {
  23. for (i = 0; i <= length; ++i) {
  24. directions[i] = NONE;
  25. }
  26.  
  27. max_time = -1;
  28.  
  29. i = count;
  30.  
  31. while (i--) {
  32. scanf("%d %c", &position, &direction);
  33.  
  34. time = falltime(position, direction, length);
  35.  
  36. if (time > max_time) {
  37. max_time = time;
  38. }
  39.  
  40. directions[position] = direction;
  41. }
  42.  
  43. last_left_going_right = length + 1;
  44. last_right_going_left = -1;
  45. last_left_going_left = -1;
  46. last_right_going_right = -1;
  47.  
  48. for (i = count, l = 0, r = length; l <= length && r >= 0; ++l, --r) {
  49. if (directions[l] == RIGHT) {
  50. last_left_going_right = l;
  51.  
  52. } else if (directions[l] == LEFT) {
  53. if (last_left_going_right < l) {
  54. directions[last_left_going_right] = NONE;
  55. --i;
  56. last_left_going_left = last_left_going_right;
  57. directions[l] = RIGHT;
  58. last_left_going_right = l;
  59.  
  60. } else {
  61. directions[l] = NONE;
  62. --i;
  63. last_left_going_left = l;
  64. }
  65. }
  66. }
  67.  
  68. for (i = count, l = 0, r = length; l <= length && r >= 0; ++l, --r) {
  69. if (directions[r] == LEFT) {
  70. last_right_going_left = r;
  71.  
  72. } else if (directions[r] == RIGHT) {
  73. if (last_right_going_left > r) {
  74. directions[last_right_going_left] = NONE;
  75. --i;
  76. last_right_going_right = last_right_going_left;
  77. directions[l] = LEFT;
  78. last_right_going_left = r;
  79.  
  80. } else {
  81. directions[r] = NONE;
  82. --i;
  83. last_right_going_right = r;
  84. }
  85. }
  86. }
  87.  
  88. printf("The last ant will fall down in %d seconds - started at ", max_time);
  89.  
  90. i = 0;
  91.  
  92. if (last_left_going_right != length + 1) {
  93. posibilities[i] = last_left_going_right;
  94. posibilities_directions[i] = RIGHT;
  95. ++i;
  96. }
  97.  
  98. if (last_left_going_left >= 0) {
  99. posibilities[i] = last_left_going_left;
  100. posibilities_directions[i] = LEFT;
  101. ++i;
  102. }
  103.  
  104. if (last_right_going_left >= 0) {
  105. posibilities[i] = last_right_going_left;
  106. posibilities_directions[i] = LEFT;
  107. ++i;
  108. }
  109.  
  110. if (last_right_going_right >= 0) {
  111. posibilities[i] = last_right_going_right;
  112. posibilities_directions[i] = RIGHT;
  113. ++i;
  114. }
  115.  
  116. max = max2 = -1;
  117. pos = pos2 = -1;
  118. i -= 1;
  119.  
  120. while (i >= 0) {
  121. //printf("[%d] = %d %c\n", i, posibilities[i], posibilities_directions[i]);
  122. time = falltime(posibilities[i], posibilities_directions[i], length);
  123. if (time >= max) {
  124. //printf("max is %d\n", posibilities[i]);
  125. max2 = max;
  126. pos2 = pos;
  127. max = time;
  128. pos = posibilities[i];
  129. }
  130. --i;
  131. }
  132.  
  133. if (max == max2 && pos != pos2) {
  134. if (pos < pos2) {
  135. printf("%d and %d.\n", pos, pos2);
  136. } else {
  137. printf("%d and %d.\n", pos2, pos);
  138. }
  139. } else {
  140. printf("%d.\n", pos);
  141. }
  142. }
  143.  
  144. return 0;
  145. }
  146.  

Diff to submission s958

ants.c

--- c4.s958.cteam044.ants.c.0.ants.c
+++ c4.s1224.cteam044.ants.c.0.ants.c
@@ -64,5 +64,7 @@
                                 }
                         }
+                }
 
+                for (i = count, l = 0, r = length; l <= length && r >= 0; ++l, --r) {
                         if (directions[r] == LEFT) {
                                 last_right_going_left = r;