Source code for submission s948

ants.cpp

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct {
  6. int position;
  7. int direction;
  8. } ant_t;
  9.  
  10. template<typename V>
  11. int bsearch(V *arr, int sz, int k) {
  12. int min = 0;
  13. int max = sz;
  14. while (max > min) {
  15. int mid = min + (max - min) / 2;
  16. int cmp = key(arr[mid]) - k;
  17.  
  18. if (cmp < 0) {
  19. min = mid + 1;
  20. } else if (cmp > 0) {
  21. max = mid;
  22. } else {
  23. return mid;
  24. }
  25. }
  26. return min;
  27. }
  28.  
  29. template<typename V>
  30. void sortInsert(V *arr, int sz, V val) {
  31. int i = bsearch<V>(arr, sz, key(val));
  32. while (i <= sz) {
  33. V tmp1 = arr[i];
  34. arr[i] = val;
  35. val = tmp1;
  36. i++;
  37. }
  38. }
  39.  
  40. enum {
  41. LEFT,
  42. RIGHT
  43. };
  44.  
  45. #define MAKE_STACK(name, sz, value_type) \
  46. struct { size_t height; value_type *vals; } name; \
  47. name.height = 0; \
  48. name.vals = (value_type*) malloc(sizeof(value_type) * sz);
  49.  
  50. #define push_sorted(name, val) \
  51. (sortInsert(name.vals, name.height, val), name.height++, (void)0)
  52.  
  53. #define push(name, val) \
  54. (name.vals[name.height++] = (val))
  55.  
  56. #define pop(name) \
  57. (name.vals[--name.height])
  58.  
  59. #define empty(name) \
  60. (name.height == 0)
  61.  
  62.  
  63.  
  64. int key(ant_t value) {
  65. return -value.position;
  66. }
  67.  
  68. #define maximize(name, val) \
  69. { if ((val) > name) name = (val); }
  70.  
  71. int main(void) {
  72. int i;
  73. int L, A;
  74.  
  75. while(scanf(" %d %d", &L, &A) == 2) {
  76. ant_t ant;
  77. char c;
  78.  
  79. MAKE_STACK(ants, A, ant_t);
  80. MAKE_STACK(ants2, A, ant_t);
  81.  
  82. for (i = 0; i < A; i++) {
  83. scanf(" %d %c", &ant.position, &c);
  84. ant.direction = (c == 'R') ? RIGHT : LEFT;
  85. push_sorted(ants, ant);
  86. push_sorted(ants2, ant);
  87. }
  88.  
  89. int lefts = 0;
  90. int rights = 0;
  91.  
  92. int max_right = -1;
  93. int max_left = -1;
  94.  
  95.  
  96. while (!empty(ants)) {
  97. ant = pop(ants);
  98.  
  99. if (ant.direction == RIGHT) {
  100. rights++;
  101.  
  102. int len = L - ant.position;
  103. if (len > max_right) {
  104. max_right = len;
  105. }
  106. } else {
  107. lefts++;
  108.  
  109. if (ant.position > max_left) {
  110. max_left = ant.position;
  111. }
  112. }
  113. }
  114.  
  115. for (int i = 0; i < lefts - 1; i++) {
  116. (void)pop(ants2);
  117. }
  118.  
  119. ant_t last_left;
  120. ant_t last_right;
  121.  
  122. last_left.position = -1;
  123. last_right.position = -1;
  124. if (lefts != 0) {
  125. last_left = pop(ants2);
  126. }
  127. if (rights != 0) {
  128. last_right = pop(ants2);
  129. }
  130.  
  131. if (max_right == max_left) {
  132. if (last_left.position > last_right.position) {
  133. int tmp = last_left.position;
  134. last_left.position = last_right.position;
  135. last_right.position = tmp;
  136. }
  137. printf("The last ant will fall down in %d seconds - started at %d and %d.\n", max_right, last_left.position, last_right.position);
  138. } else if (max_right > max_left) {
  139. printf("The last ant will fall down in %d seconds - started at %d.\n", max_right, last_right.position);
  140. } else {
  141. printf("The last ant will fall down in %d seconds - started at %d.\n", max_left, last_left.position);
  142. }
  143. }
  144.  
  145. return 0;
  146. }
  147.