Source code for submission s1128

Go to diff to previous submission

ants.cpp

  1. /*
  2.  * File: ants.c
  3.  * Author: cteam057
  4.  *
  5.  * Created on October 27, 2012, 12:39 PM
  6.  */
  7.  
  8. #include <cstdlib>
  9. #include <cstdio>
  10.  
  11. /*
  12.  *
  13.  */
  14.  
  15.  
  16. int cycles, l;
  17.  
  18. class Ant {
  19. public:
  20. double state, pos;
  21. int start, moved;
  22. Ant() {
  23. state = pos = start = moved = 0;
  24. }
  25. void operator=(Ant &b) {
  26. start=b.start;
  27. pos=b.pos;
  28. moved=b.moved;
  29. state=b.state;
  30. }
  31. };
  32.  
  33. int comp(const void* a, const void* b) {
  34. Ant* bx = (Ant *)b;
  35. Ant* ax = (Ant *)a;
  36. if(ax->start<bx->start) return -1;
  37. if(ax->start==bx->start) return 0;
  38. return 1;
  39. }
  40.  
  41.  
  42. int main(int argc, char** argv) {
  43. int a;
  44. int i, p, j, mov;
  45. char d;
  46. Ant *ants;
  47.  
  48. while(scanf("%d %d", &l, &a)==2) {
  49. ants = new Ant[a];
  50. cycles=0;
  51. for(i = 0; i < a; i++) {
  52. scanf("%d %c", &p, &d);
  53. ants[i].start=p;
  54. ants[i].pos=p;
  55. if(d == 'L') {
  56. ants[i].state = -0.5;
  57. if(p>cycles) cycles = p;
  58. } else {
  59. ants[i].state = 0.5;
  60. if((l-p)>cycles) cycles = l-p;
  61. };
  62. }
  63.  
  64. qsort(ants, a, sizeof(Ant), comp );
  65.  
  66. i = 0;
  67. while(i < cycles*2) {
  68. mov = l;
  69. for(j = 1; j<a; j++) {
  70. if(ants[j].state<0&&ants[j-1].state>0
  71. && ants[j].pos-ants[j-1].pos<mov) mov = ants[j].pos-ants[j-1].pos;
  72. }
  73.  
  74. ants[0].pos += ants[0].state*mov;
  75. if(ants[0].state) ants[0].moved = 1; else ants[0].moved = 0;
  76. if(ants[0].pos<=0 || ants[0].pos>=l) ants[0].state=0;
  77. for(j = 1; j < a; j++) {
  78. ants[j].pos += ants[j].state*mov;
  79. if(ants[j].state) ants[j].moved = 1; else ants[j].moved = 0;
  80. if(ants[j].pos==ants[j-1].pos) {
  81. ants[j].state*=-1;
  82. ants[j-1].state*=-1;
  83. }
  84. if(ants[j].pos<=0 || ants[j].pos>=l) ants[j].state=0;
  85. }
  86. i+=mov;
  87. }
  88.  
  89. if(cycles==0) {
  90. if(ants[0].state<0) ants[0].moved=1;
  91. for(i = 1; i<a-1; i++) {
  92. ants[i].moved=0;
  93. }
  94. if(ants[a-1].state>0) ants[a-1].moved=1;
  95. }
  96.  
  97. printf("The last ant will fall down in %d seconds - started at ", cycles);
  98. j = 0;
  99. for(i = 0; i < a; i++) {
  100. if(ants[i].moved&&j==0) {
  101. j++;
  102. printf("%d", ants[i].start);
  103. continue;
  104. }
  105. if(ants[i].moved&&j==1) {
  106. printf(" and %d", ants[i].start);
  107. break;
  108. }
  109. }
  110. printf(".\n");
  111.  
  112. delete ants;
  113. }
  114. return 0;
  115. }
  116.  
  117.  

Diff to submission s1110

ants.cpp

--- c4.s1110.cteam057.ants.cpp.0.ants.cpp
+++ c4.s1128.cteam057.ants.cpp.0.ants.cpp
@@ -42,5 +42,5 @@
 int main(int argc, char** argv) {
     int a;
-    int i, p, j;
+    int i, p, j, mov;
     char d;
     Ant *ants;
@@ -64,11 +64,17 @@
         qsort(ants, a, sizeof(Ant), comp );
         
-        
-        for(i = 0; i < cycles*2; i++) {
-            ants[0].pos += ants[0].state;
+        i = 0;
+        while(i < cycles*2) {
+            mov = l;
+            for(j = 1; j<a; j++) {
+                if(ants[j].state<0&&ants[j-1].state>0
+                        && ants[j].pos-ants[j-1].pos<mov) mov = ants[j].pos-ants[j-1].pos;
+            }
+            
+            ants[0].pos += ants[0].state*mov;
             if(ants[0].state) ants[0].moved = 1; else ants[0].moved = 0;
             if(ants[0].pos<=0 || ants[0].pos>=l) ants[0].state=0;
             for(j = 1; j < a; j++) {
-                ants[j].pos += ants[j].state;
+                ants[j].pos += ants[j].state*mov;
                 if(ants[j].state) ants[j].moved = 1; else ants[j].moved = 0;
                 if(ants[j].pos==ants[j-1].pos) {
@@ -78,4 +84,5 @@
                 if(ants[j].pos<=0 || ants[j].pos>=l) ants[j].state=0;
             }
+            i+=mov;
         }