Source code for submission s964

Go to diff to previous submission

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. enum {
  11. LEFT,
  12. RIGHT
  13. };
  14.  
  15. int cmp(const void *v1, const void *v2) {
  16. ant_t *a1 = (ant_t*) v1;
  17. ant_t *a2 = (ant_t*) v2;
  18. return (a1->position - a2->position);
  19. }
  20.  
  21. #define maximize(name, val) \
  22. { if ((val) > name) name = (val); }
  23.  
  24. int main(void) {
  25. int i;
  26. int L, A;
  27.  
  28. while(scanf(" %d %d", &L, &A) == 2) {
  29. ant_t ant;
  30. char c;
  31.  
  32. ant_t *ants = (ant_t*)malloc(sizeof(ant_t) * A);
  33. for (i = 0; i < A; i++) {
  34. scanf(" %d %c", &ant.position, &c);
  35. ant.direction = (c == 'R') ? RIGHT : LEFT;
  36.  
  37. ants[i] = ant;
  38. }
  39.  
  40. qsort(ants, A, sizeof(ant_t), cmp);
  41.  
  42. int lefts = 0;
  43. int rights = 0;
  44.  
  45. int max_right = -1;
  46. int max_left = -1;
  47.  
  48. for (int i = 0; i < A; i++) {
  49. ant = ants[i];
  50.  
  51. if (ant.direction == RIGHT) {
  52. rights++;
  53.  
  54. int len = L - ant.position;
  55. if (len > max_right) {
  56. max_right = len;
  57. }
  58. } else {
  59. lefts++;
  60.  
  61. if (ant.position > max_left) {
  62. max_left = ant.position;
  63. }
  64. }
  65. }
  66.  
  67. ant_t last_left;
  68. ant_t last_right;
  69.  
  70. last_left.position = -1;
  71. last_right.position = -1;
  72. if (lefts != 0) {
  73. last_left = ants[lefts-1];
  74. }
  75. if (rights != 0) {
  76. last_right = ants[lefts];
  77. }
  78.  
  79. if (max_right == max_left) {
  80. if (last_left.position > last_right.position) {
  81. int tmp = last_left.position;
  82. last_left.position = last_right.position;
  83. last_right.position = tmp;
  84. }
  85. printf("The last ant will fall down in %d seconds - started at %d and %d.\n", max_right, last_left.position, last_right.position);
  86. } else if (max_right > max_left) {
  87. printf("The last ant will fall down in %d seconds - started at %d.\n", max_right, last_right.position);
  88. } else {
  89. printf("The last ant will fall down in %d seconds - started at %d.\n", max_left, last_left.position);
  90. }
  91. }
  92.  
  93. return 0;
  94. }
  95.  

Diff to submission s948

ants.cpp

--- c4.s948.cteam091.ants.cpp.0.ants.cpp
+++ c4.s964.cteam091.ants.cpp.0.ants.cpp
@@ -8,34 +8,4 @@
 } ant_t;
 
-template<typename V>
-int bsearch(V *arr, int sz, int k) {
-        int min = 0;
-        int max = sz;
-        while (max > min) {
-                int mid = min + (max - min) / 2;
-                int cmp = key(arr[mid]) - k;
-                
-                if (cmp < 0) {
-                        min = mid + 1;
-                } else if (cmp > 0) {
-                        max = mid;
-                } else {
-                        return mid;
-                }
-        }
-        return min;
-}
-
-template<typename V>
-void sortInsert(V *arr, int sz, V val) {
-        int i = bsearch<V>(arr, sz, key(val));
-        while (i <= sz) {
-                V tmp1 = arr[i];
-                arr[i] = val;
-                val = tmp1;
-                i++;
-        }
-}
-
 enum {
         LEFT,
@@ -43,25 +13,8 @@
 };
 
-#define MAKE_STACK(name, sz, value_type) \
-        struct { size_t height; value_type *vals; } name; \
-        name.height = 0; \
-        name.vals = (value_type*) malloc(sizeof(value_type) * sz);
-
-#define push_sorted(name, val) \
-        (sortInsert(name.vals, name.height, val), name.height++, (void)0)
-
-#define push(name, val) \
-        (name.vals[name.height++] = (val))
-
-#define pop(name) \
-        (name.vals[--name.height])
-
-#define empty(name) \
-        (name.height == 0)
-
-
-
-int key(ant_t value) {
-        return -value.position;
+int cmp(const void *v1, const void *v2) {
+        ant_t *a1 = (ant_t*) v1;
+        ant_t *a2 = (ant_t*) v2;
+        return (a1->position - a2->position);
 }
 
@@ -77,13 +30,13 @@
                 char c;
                 
-                MAKE_STACK(ants, A, ant_t);
-                MAKE_STACK(ants2, A, ant_t);
-                
+                ant_t *ants = (ant_t*)malloc(sizeof(ant_t) * A);
                 for (i = 0; i < A; i++) {
                         scanf(" %d %c", &ant.position, &c);
                         ant.direction = (c == 'R') ? RIGHT : LEFT;
-                        push_sorted(ants, ant);
-                        push_sorted(ants2, ant);
+                        
+                        ants[i] = ant;
                 }
+
+                qsort(ants, A, sizeof(ant_t), cmp);
                 
                 int lefts = 0;
@@ -93,7 +46,6 @@
                 int max_left = -1;
                 
-                
-                while (!empty(ants)) {
-                        ant = pop(ants);
+                for (int i = 0; i < A; i++) {
+                        ant = ants[i];
                         
                         if (ant.direction == RIGHT) {
@@ -113,8 +65,4 @@
                 }
                 
-                for (int i = 0; i < lefts - 1; i++) {
-                        (void)pop(ants2);
-                }
-
                 ant_t last_left;
                 ant_t last_right;
@@ -123,8 +71,8 @@
                 last_right.position = -1;
                 if (lefts != 0) {
-                        last_left = pop(ants2);
+                        last_left = ants[lefts-1];
                 }
                 if (rights != 0) {
-                        last_right = pop(ants2);
+                        last_right = ants[lefts];
                 }