Source code for submission s1313

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. if (directions[r] == LEFT) {
  68. last_right_going_left = r;
  69.  
  70. } else if (directions[r] == RIGHT) {
  71. if (last_right_going_left > r) {
  72. directions[last_right_going_left] = NONE;
  73. --i;
  74. last_right_going_right = last_right_going_left;
  75. directions[r] = LEFT;
  76. last_right_going_left = r;
  77.  
  78. } else {
  79. directions[r] = NONE;
  80. --i;
  81. last_right_going_right = r;
  82. }
  83. }
  84. }
  85.  
  86. printf("The last ant will fall down in %d seconds - started at ", max_time);
  87.  
  88. i = 0;
  89.  
  90. if (last_left_going_right != length + 1) {
  91. posibilities[i] = last_left_going_right;
  92. posibilities_directions[i] = RIGHT;
  93. ++i;
  94. }
  95.  
  96. if (last_left_going_left >= 0) {
  97. posibilities[i] = last_left_going_left;
  98. posibilities_directions[i] = LEFT;
  99. ++i;
  100. }
  101.  
  102. if (last_right_going_left >= 0) {
  103. posibilities[i] = last_right_going_left;
  104. posibilities_directions[i] = LEFT;
  105. ++i;
  106. }
  107.  
  108. if (last_right_going_right >= 0) {
  109. posibilities[i] = last_right_going_right;
  110. posibilities_directions[i] = RIGHT;
  111. ++i;
  112. }
  113.  
  114. max = max2 = -1;
  115. pos = pos2 = -1;
  116. i -= 1;
  117.  
  118. while (i >= 0) {
  119. //printf("[%d] = %d %c\n", i, posibilities[i], posibilities_directions[i]);
  120. time = falltime(posibilities[i], posibilities_directions[i], length);
  121. if (time >= max) {
  122. //printf("max is %d\n", posibilities[i]);
  123. max2 = max;
  124. pos2 = pos;
  125. max = time;
  126. pos = posibilities[i];
  127. }
  128. --i;
  129. }
  130.  
  131. if (max == max2 && pos != pos2) {
  132. if (pos < pos2) {
  133. printf("%d and %d.\n", pos, pos2);
  134. } else {
  135. printf("%d and %d.\n", pos2, pos);
  136. }
  137. } else {
  138. printf("%d.\n", pos);
  139. }
  140. }
  141.  
  142. return 0;
  143. }
  144.  

Diff to submission s1224

ants.c

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