Source code for submission s1234

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. Ant ant[100000];
  39. int ants, pos, len, i, j;
  40. int maxTime=-1, maxAnt1=-1, maxAnt2=-1;
  41. char dir[3];
  42. while(scanf("%d %d", &len, &ants)>0) {
  43. memset(ant, 0, (len+1)*sizeof(Ant));
  44.  
  45. for(i=0; i<ants; i++) {
  46. scanf("%d %s", &pos, dir);
  47. ant[pos].id = pos;
  48. ant[pos].dir = (dir[0]=='L' ? -1 : 1);
  49. ant[pos].time = (ant[pos].dir==-1 ? pos : len-pos);
  50. }
  51.  
  52. for(i=0,j=0; i<=len; i++) {
  53. if(i!=j && ant[i].dir) {
  54. memcpy(ant+j,ant+i, sizeof(Ant));
  55. j++;
  56. }
  57. }
  58.  
  59. for(i=0; i<ants-1; i++) {
  60. j=i;
  61. while(ant[j].compare(ant+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[i].time == maxTime) {
  72. maxAnt2 = ant[i].id;
  73. }else if (ant[i].time > maxTime) {
  74. maxAnt1 = ant[i].id;
  75. maxAnt2 = -1;
  76. maxTime = ant[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 s1142

ants2.cpp

--- c4.s1142.cteam102.ants.cpp.0.ants2.cpp
+++ c4.s1234.cteam102.ants.cpp.0.ants.cpp
@@ -1,105 +1,89 @@
-#include <cstdio>
-#include <cstring>
+#include <stdio.h>
+#include <algorithm>
+#include <string.h>
 
-#include <cstdlib>
+#define debug(format,...) fprintf(stderr, format, __VA_ARGS__)
+class Ant;
+bool operator< (const Ant&, const Ant &);
 
-struct Ant {
-        int dir;
-        int start;
+class Ant {
+        public:
+                int dir;
+                int id;
+                int time;
+                friend bool operator< (const Ant&, const Ant &);
+/*              bool operator<= (const Ant &ant) {
+                        return this->id <= ant.id;
+                }*/
+                void reverse(Ant *ant) {
+                        std::swap<int>(this->time, ant->time);
+                        this->dir *= -1;
+                        ant->dir *= -1;
+                }
+                bool compare(Ant *ant) {
+                        if(this->dir==1 && ant->dir==-1) {
+                                this->reverse(ant);
+                                return true;
+                        }
+                        return false;
+                }
 };
 
-int main()
-{
-        int ants, pos, woodlen;
-        char dir;
-        while(scanf("%d %d", &woodlen, &ants)>0) 
-        {
-                Ant *wood[woodlen];
-                memset (wood,NULL,woodlen*sizeof(Ant*));
+bool operator< (const Ant &ant1, const Ant &ant2) {
+        return ant1.id < ant2.id;
+}
+
+
+int main() {
+        Ant ant[100000];
+        int ants, pos, len, i, j;
+        int maxTime=-1, maxAnt1=-1, maxAnt2=-1;
+        char dir[3];
+        while(scanf("%d %d", &len, &ants)>0) {
+                memset(ant, 0, (len+1)*sizeof(Ant));
                 
-                for(int i=0; i<ants; i++) 
-                {
-                        scanf("%d %c", &pos, &dir);
-                        Ant *a = new Ant;
-                        if(dir=='R')
-                                a->dir = 1;
-                        else
-                                a->dir = -1;
-                        a->start=pos;
-                        
-                        wood[pos]=a;
-                        printf( "%d\n", a->dir );
+                for(i=0; i<ants; i++) {
+                        scanf("%d %s", &pos, dir);
+                        ant[pos].id = pos;
+                        ant[pos].dir = (dir[0]=='L' ? -1 : 1);
+                        ant[pos].time = (ant[pos].dir==-1 ? pos : len-pos);
                 }
-                                
-                int time=1;
-                Ant * Lf,*Rf;
-                while (ants >0)
-                {
-                        Lf=NULL;
-                        Rf=NULL;
-                        
-                        if(( wood[0] != NULL ) && ( wood[0]->dir==-1))//L
-                        {
-                                Lf=wood[0];
-                                wood[0]=NULL;
-                                ants--;
-                                
-                        }
-                        
-                        if((wood[woodlen-1]!=NULL)&&(wood[woodlen-1]->dir==1))//P
-                        {
-                                Rf=wood[woodlen-1];
-                                wood[woodlen-1]=NULL;
-                                ants--;
-                        }
                 
-                        for (int i=1 ; i<woodlen-1; i++)
-                        {
-                        
-
-                                if(wood[i]!=NULL)
-                                {
-                                        if(wood[i+wood[i]->dir]!=NULL) //jetam
-                                                wood[i]->dir*=-1;
-                                        else
-                                        {
-                                            //printf("Dir %d", wood[i]->dir );
-                                                wood[i+wood[i]->dir] = wood[i];
-                                                wood[i]=NULL;
-                                                i++;
-                                        }
-                                }
+                for(i=0,j=0; i<=len; i++) {
+                        if(i!=j && ant[i].dir) {
+                                memcpy(ant+j,ant+i, sizeof(Ant));
+                                j++;
                         }
-                        
-                        time++;
-                        
                 }
                 
-                if(Rf!=NULL && Lf!=NULL)
-                {
-                        int a = Rf->start,b = Lf->start;
-                        
-                        if(Rf->start > Lf->start)
-                        {
-                                a=Lf->start;
-                                b=Rf->start;    
+                for(i=0; i<ants-1; i++) {
+                        j=i;
+                        while(ant[j].compare(ant+j+1) && j>0) {
+                                if(j==0) {
+                                        break;
+                                }
+                                j--;
                         }
-                
-                        printf("The last will fall down in %d seconds - started at %d and %d.\n", time,a,b);
-                
                 }
                 
-                else
-                {
-                        if(Rf!=NULL)
-                        {
-                                printf("The last will fall down in %d seconds - started at %d\n", time,Rf->start);
-                        }
-                        else
-                        {
-                                printf("The last will fall down in %d seconds - started at %d\n", time,Lf->start);
+                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;
+                                maxAnt2 = -1;
+                                maxTime = ant[i].time;
                         }
                 }
-        
+                if (maxAnt2 == -1) {
+                        printf(
+                                "The last ant will fall down in %d seconds - started at %d.\n",
+                                maxTime, maxAnt1);
+                }else {
+                        printf(
+                                "The last ant will fall down in %d seconds - started at %d and %d.\n",
+                                maxTime, maxAnt1, maxAnt2);
+                }
         }
         return 0;