Source code for submission s1250

Go to diff to previous submission

ants.cpp

  1. #include <iomanip>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstdio>
  5. //#include <stdlib.h>
  6.  
  7. using namespace std;
  8. struct elem{
  9. int idx;
  10. int dir;
  11. };
  12.  
  13. int cmp(const void * x, const void * y){
  14. elem *t1 = (elem*) x;
  15. elem *t2 = (elem*) y;
  16. return (t1->idx) - (t2->idx);
  17. }
  18.  
  19.  
  20. int main(){
  21. int number,length;
  22.  
  23. scanf("%d %d", &length, &number);
  24. int *start_pos = new int[number];
  25. int *act_pos = new int[number];
  26.  
  27. elem *pole = new elem [number];
  28.  
  29. int *dir = new int[number];
  30. for (int i=0; i < number; i++)
  31. start_pos[i]=0;
  32.  
  33. int *len=new int[number];
  34. int pos;
  35.  
  36. // read all inputs && count time for each ant
  37. for (int i=0; i < number; i++){
  38. char tmp;
  39. scanf("%d %c", &pos, &tmp);
  40. if (tmp == 'R' ){
  41. len[i]=length-pos;
  42. dir[i]=1;
  43. pole[i].dir=1;
  44.  
  45. }
  46. else {
  47. len[i]=pos;
  48. dir[i]=-1;
  49. pole[i].dir=-1;
  50. }
  51. pole[i].idx=pos;
  52. start_pos[i]=pos;
  53. act_pos[i]=pos;
  54. }
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. // find max
  63. int max = len[0];
  64. for (int i=1; i < number; i++){
  65. if (len[i] > max){
  66. max = len[i];
  67. }
  68. }
  69.  
  70. //cout << "max found" << endl;
  71.  
  72. int *fin = new int [number];
  73. for (int i=0; i < number; i++)
  74. if (len[i] == -1);
  75.  
  76.  
  77. int nr=0;
  78. for (int i=0; i < number; i++){
  79. if (len[i] == max){
  80. fin[nr++]=i;
  81. }
  82. }
  83.  
  84.  
  85. int *used_pos = new int [length];
  86. for (int i=0; i < number; i++)
  87. used_pos[i]=0;
  88.  
  89. bool *alive = new bool [number];
  90. int aliveAnts=number;
  91. bool *preMoved = new bool [number];
  92.  
  93. int *id = new int [number];
  94.  
  95. // setup
  96. for (int i=0; i < number; i++){
  97. alive[i]=true;
  98. preMoved[i]=false;
  99. id[i]=i;
  100. }
  101.  
  102.  
  103. int number2=number;
  104. qsort(pole, number, sizeof(elem), cmp);
  105. number=number2;
  106.  
  107.  
  108. for (int i=0; i < number; i++){
  109. act_pos[i]=start_pos[i]=pole[i].idx;
  110. dir[i]=pole[i].dir;
  111.  
  112. }
  113.  
  114.  
  115. if (number > 1)
  116. for (int k=0; k < max-1; k++){
  117. //cout << "-------------" << endl;
  118. //cout << "time: " << k << endl;
  119.  
  120. for (int i=0; i < number; i++)
  121. preMoved[i]=false;
  122.  
  123. //distance = 1, direction = crash ===> change direction, do NOT change actual position!
  124. for (int i=0; i < number-1; i++){
  125. if ((alive[i]==false)||(alive[i+1]==false))
  126. continue;
  127. if ((act_pos[i]+dir[i]==(act_pos[i+1])) && ((act_pos[i])==(act_pos[i+1]+dir[i+1]))){
  128. // cout << "crash 1 ants " << i << " and " << i+1 << " on pos " << act_pos[i] << "(+1)" << endl;
  129. preMoved[i]=true;
  130. dir[i]*=-1;
  131. preMoved[i+1]=true;
  132. dir[i+1]*=-1;
  133. }
  134. }
  135.  
  136.  
  137. // move
  138. for (int i=0; i < number; i++){
  139. if (preMoved[i]==true){
  140. // cout << "ant " << i << " is on pos " << act_pos[i] << endl;
  141. continue;
  142. }
  143. act_pos[i]+=dir[i];
  144. // cout << "ant " << i << " is on pos " << act_pos[i] << endl;
  145. }
  146.  
  147. // check for death ants
  148. for (int i=0; i < number; i++){
  149. if ((alive[i]==true)&&((act_pos[i]>=length)||(act_pos[i]<0))){
  150. alive[i]=false;
  151. aliveAnts--;
  152. // cout << "ant " << i << " died on pos " << act_pos[i] << endl;
  153. }
  154. }
  155.  
  156.  
  157. for (int i=0; i < length; i++){
  158. used_pos[i]=0;
  159. }
  160.  
  161. // distance = 2, direction = crash
  162. for (int i=0; i < number; i++){
  163. if (alive[i]==false)
  164. continue;
  165. used_pos[act_pos[i]]++;
  166. }
  167.  
  168. // if on 'same' pos => crash => change direction
  169. for (int i=0; i < length; i++){
  170. if (used_pos[i] > 1){
  171. for (int j=0; j < number; j++){
  172. if (act_pos[j]==i){
  173. dir[j]*=-1;
  174. // cout << "changing dir of ant " << j << " into " << dir[j] << endl;
  175. }
  176. }
  177. }
  178. }
  179.  
  180. }
  181.  
  182.  
  183. int dfas=0;
  184. for (int i=0; i < number; i++)
  185. if (alive[i]==true){
  186. // cout << i << " is alive" << " on start pos" << start_pos[i] << endl;
  187. fin[dfas++]=i;
  188. }
  189. //cout << "before output" << endl;
  190.  
  191.  
  192.  
  193. cout << "The last ant will fall down in " << max << " seconds -" << " started at " << start_pos [fin[0]];
  194.  
  195. for (int i=1; i < nr; i++){
  196. cout << " and " << start_pos [fin[i]];
  197. }
  198.  
  199. cout << "."<<endl;
  200.  
  201. return 0;
  202. }
  203.  
  204.  

Diff to submission s1038

ants.cpp

--- c4.s1038.cteam015.ants.cpp.0.ants.cpp
+++ c4.s1250.cteam015.ants.cpp.0.ants.cpp
@@ -3,6 +3,18 @@
 #include <cstdlib>
 #include <cstdio>
+//#include <stdlib.h>
 
 using namespace std;
+struct elem{
+int idx;
+int dir;
+}; 
+
+int cmp(const void * x, const void * y){
+elem *t1 = (elem*) x;
+elem *t2 = (elem*) y;
+return (t1->idx) - (t2->idx);
+}
+
 
 int main(){
@@ -12,4 +24,7 @@
 int *start_pos = new int[number];
 int *act_pos = new int[number];
+
+elem *pole = new elem [number];
+
 int *dir = new int[number];
 for  (int i=0; i < number; i++)
@@ -23,7 +38,8 @@
         char tmp;
         scanf("%d %c", &pos, &tmp);
-        if  (tmp == 'R' ){
+       if  (tmp == 'R' ){
                 len[i]=length-pos;
                 dir[i]=1;
+                pole[i].dir=1;
                 
         }
@@ -31,26 +47,11 @@
                 len[i]=pos;
                 dir[i]=-1;
+                pole[i].dir=-1;
         }
+        pole[i].idx=pos;
         start_pos[i]=pos;
         act_pos[i]=pos;
 }
 
-for  (int i=0; i < number; i++){
-        int max=start_pos[0];
-        for  (int j=0; j < number-1-i; j++){
-                if (start_pos[i] > max){
-                        int tmp=start_pos[i];
-                        start_pos[i]=start_pos[i+1];
-                        start_pos[i+1]=tmp;
-                        tmp= act_pos[i];
-                        act_pos[i]=act_pos[i+1];
-                        act_pos[i+1]=tmp;
-                        tmp= dir[i];
-                        dir[i]=dir[i+1];
-                        dir[i+1]=tmp;
-                }
-                        
-        }
-}
 
 
@@ -99,14 +100,31 @@
 }
 
+
+int number2=number;
+qsort(pole, number, sizeof(elem), cmp);
+number=number2;
+
+
+for  (int i=0; i < number; i++){
+        act_pos[i]=start_pos[i]=pole[i].idx;
+        dir[i]=pole[i].dir;
+
+}
+
+
+if (number > 1)
 for (int k=0; k < max-1; k++){
 //cout << "-------------" << endl;
 //cout << "time: " << k  << endl;
 
-// distance = 1, direction = crash ===> change direction, do NOT change actual position!
+for  (int i=0; i < number; i++)
+        preMoved[i]=false;
+
+//distance = 1, direction = crash ===> change direction, do NOT change actual position!
 for  (int i=0; i < number-1; i++){
         if ((alive[i]==false)||(alive[i+1]==false))
                 continue;
         if ((act_pos[i]+dir[i]==(act_pos[i+1])) && ((act_pos[i])==(act_pos[i+1]+dir[i+1]))){
-                //cout << "crash 1 ants " << i << " and " << i+1 << " on pos " <<  act_pos[i] << "(+1)" << endl;
+//              cout << "crash 1 ants " << i << " and " << i+1 << " on pos " <<  act_pos[i] << "(+1)" << endl;
                 preMoved[i]=true;
                 dir[i]*=-1;
@@ -119,8 +137,10 @@
 // move
 for  (int i=0; i < number; i++){
-        if (preMoved[i]==true)
+        if (preMoved[i]==true){
+//              cout << "ant " << i << " is on pos " << act_pos[i] << endl;
                 continue;
+        }
         act_pos[i]+=dir[i];
-        //cout << "ant " << i << " is on pos " << act_pos[i] << endl;
+//      cout << "ant " << i << " is on pos " << act_pos[i] << endl;
 }
 
@@ -130,5 +150,5 @@
                 alive[i]=false;
                 aliveAnts--;
-                //cout << "ant " << i << " died on pos " << act_pos[i] << endl;
+//              cout << "ant " << i << " died on pos " << act_pos[i] << endl;
         }
 }
@@ -152,5 +172,5 @@
                         if (act_pos[j]==i){
                                 dir[j]*=-1;     
-                                //cout << "changing dir of ant " << j << " into "        << dir[j] << endl;
+//                              cout << "changing dir of ant " << j << " into "  << dir[j] << endl;
                         }
                 }
@@ -161,4 +181,10 @@
 
 
+int dfas=0;
+for  (int i=0; i < number; i++)
+        if (alive[i]==true){
+//              cout << i << " is alive" << " on start pos" << start_pos[i] <<  endl;
+        fin[dfas++]=i;
+        }
 //cout << "before output" << endl;