Source code for submission s1279

Go to diff to previous submission

ants.cpp

  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <string.h>
  4.  
  5. #define debug(format,...) fprintf(stderr, format, __VA_ARGS__)
  6. class Ant;
  7. bool operator< (const Ant&, const Ant &);
  8.  
  9. class Ant {
  10. public:
  11. int dir;
  12. int id;
  13. int time;
  14. friend bool operator< (const Ant&, const Ant &);
  15. /* bool operator<= (const Ant &ant) {
  16. return this->id <= ant.id;
  17. }*/
  18. void reverse(Ant *ant) {
  19. std::swap<int>(this->time, ant->time);
  20. this->dir *= -1;
  21. ant->dir *= -1;
  22. }
  23. bool compare(Ant *ant) {
  24. if(this->dir==1 && ant->dir==-1) {
  25. this->reverse(ant);
  26. return true;
  27. }
  28. return false;
  29. }
  30. };
  31.  
  32. bool operator< (const Ant &ant1, const Ant &ant2) {
  33. return ant1.id < ant2.id;
  34. }
  35.  
  36.  
  37. int main() {
  38. int positions[100000];
  39. Ant ant[100000];
  40. int ants, pos, len, i, j;
  41. int maxTime=-1, maxAnt1=-1, maxAnt2=-1;
  42. char dir[3];
  43. while(scanf("%d %d", &len, &ants)>0) {
  44. memset(ant, 0, (len+1)*sizeof(Ant));
  45.  
  46. for(i=0; i<ants; i++) {
  47. scanf("%d %s", &pos, dir);
  48. ant[pos].id = pos;
  49. ant[pos].dir = (dir[0]=='L' ? -1 : 1);
  50. ant[pos].time = (ant[pos].dir==-1 ? pos : len-pos);
  51. }
  52.  
  53. for(i=0,j=0; i<=len; i++) {
  54. if(i!=j && ant[i].dir) {
  55. positions[j++]=i;
  56. }
  57. }
  58.  
  59. for(i=0; i<ants-1; i++) {
  60. j=i;
  61. while(ant[positions[j]].compare(ant+positions[j+1]) && j>0) {
  62. if(j==0) {
  63. break;
  64. }
  65. j--;
  66. }
  67. }
  68.  
  69. maxTime = maxAnt2 = -1;
  70. for(i=0; i<ants; i++) {
  71. if(ant[positions[i]].time == maxTime) {
  72. maxAnt2 = ant[positions[i]].id;
  73. }else if (ant[positions[i]].time > maxTime) {
  74. maxAnt1 = ant[positions[i]].id;
  75. maxAnt2 = -1;
  76. maxTime = ant[positions[i]].time;
  77. }
  78. }
  79. if (maxAnt2 == -1) {
  80. printf(
  81. "The last ant will fall down in %d seconds - started at %d.\n",
  82. maxTime, maxAnt1);
  83. }else {
  84. printf(
  85. "The last ant will fall down in %d seconds - started at %d and %d.\n",
  86. maxTime, maxAnt1, maxAnt2);
  87. }
  88. }
  89. return 0;
  90. }
  91.  
  92.  

Diff to submission s1234

ants.cpp

--- c4.s1234.cteam102.ants.cpp.0.ants.cpp
+++ c4.s1279.cteam102.ants.cpp.0.ants.cpp
@@ -36,4 +36,5 @@
 
 int main() {
+        int positions[100000];
         Ant ant[100000];
         int ants, pos, len, i, j;
@@ -52,6 +53,5 @@
                 for(i=0,j=0; i<=len; i++) {
                         if(i!=j && ant[i].dir) {
-                                memcpy(ant+j,ant+i, sizeof(Ant));
-                                j++;
+                                positions[j++]=i;
                         }
                 }
@@ -59,5 +59,5 @@
                 for(i=0; i<ants-1; i++) {
                         j=i;
-                        while(ant[j].compare(ant+j+1) && j>0) {
+                        while(ant[positions[j]].compare(ant+positions[j+1]) && j>0) {
                                 if(j==0) {
                                         break;
@@ -69,10 +69,10 @@
                 maxTime = maxAnt2 = -1;
                 for(i=0; i<ants; i++) {
-                        if(ant[i].time == maxTime) {
-                                maxAnt2 = ant[i].id;
-                        }else if (ant[i].time > maxTime) {
-                                maxAnt1 = ant[i].id;
+                        if(ant[positions[i]].time == maxTime) {
+                                maxAnt2 = ant[positions[i]].id;
+                        }else if (ant[positions[i]].time > maxTime) {
+                                maxAnt1 = ant[positions[i]].id;
                                 maxAnt2 = -1;
-                                maxTime = ant[i].time;
+                                maxTime = ant[positions[i]].time;
                         }
                 }