Source code for submission s1142

Go to diff to previous submission

ants2.cpp

  1. #include <cstdio>
  2. #include <cstring>
  3.  
  4. #include <cstdlib>
  5.  
  6. struct Ant {
  7. int dir;
  8. int start;
  9. };
  10.  
  11. int main()
  12. {
  13. int ants, pos, woodlen;
  14. char dir;
  15. while(scanf("%d %d", &woodlen, &ants)>0)
  16. {
  17. Ant *wood[woodlen];
  18. memset (wood,NULL,woodlen*sizeof(Ant*));
  19.  
  20. for(int i=0; i<ants; i++)
  21. {
  22. scanf("%d %c", &pos, &dir);
  23. Ant *a = new Ant;
  24. if(dir=='R')
  25. a->dir = 1;
  26. else
  27. a->dir = -1;
  28. a->start=pos;
  29.  
  30. wood[pos]=a;
  31. printf( "%d\n", a->dir );
  32. }
  33.  
  34. int time=1;
  35. Ant * Lf,*Rf;
  36. while (ants >0)
  37. {
  38. Lf=NULL;
  39. Rf=NULL;
  40.  
  41. if(( wood[0] != NULL ) && ( wood[0]->dir==-1))//L
  42. {
  43. Lf=wood[0];
  44. wood[0]=NULL;
  45. ants--;
  46.  
  47. }
  48.  
  49. if((wood[woodlen-1]!=NULL)&&(wood[woodlen-1]->dir==1))//P
  50. {
  51. Rf=wood[woodlen-1];
  52. wood[woodlen-1]=NULL;
  53. ants--;
  54. }
  55.  
  56. for (int i=1 ; i<woodlen-1; i++)
  57. {
  58.  
  59.  
  60. if(wood[i]!=NULL)
  61. {
  62. if(wood[i+wood[i]->dir]!=NULL) //jetam
  63. wood[i]->dir*=-1;
  64. else
  65. {
  66. //printf("Dir %d", wood[i]->dir );
  67. wood[i+wood[i]->dir] = wood[i];
  68. wood[i]=NULL;
  69. i++;
  70. }
  71. }
  72. }
  73.  
  74. time++;
  75.  
  76. }
  77.  
  78. if(Rf!=NULL && Lf!=NULL)
  79. {
  80. int a = Rf->start,b = Lf->start;
  81.  
  82. if(Rf->start > Lf->start)
  83. {
  84. a=Lf->start;
  85. b=Rf->start;
  86. }
  87.  
  88. printf("The last will fall down in %d seconds - started at %d and %d.\n", time,a,b);
  89.  
  90. }
  91.  
  92. else
  93. {
  94. if(Rf!=NULL)
  95. {
  96. printf("The last will fall down in %d seconds - started at %d\n", time,Rf->start);
  97. }
  98. else
  99. {
  100. printf("The last will fall down in %d seconds - started at %d\n", time,Lf->start);
  101. }
  102. }
  103.  
  104. }
  105. return 0;
  106. }
  107.  

Diff to submission s823

ants.cpp

--- c4.s823.cteam102.ants.cpp.0.ants.cpp
+++ c4.s1142.cteam102.ants.cpp.0.ants2.cpp
@@ -1,79 +1,105 @@
-#include <stdio.h>
-#include <algorithm>
+#include <cstdio>
+#include <cstring>
 
-#define debug(format,...) fprintf(stderr, format, __VA_ARGS__)
-class Ant;
-bool operator< (const Ant&, const Ant &);
+#include <cstdlib>
 
-class Ant {
-        public:
-                bool left;
-                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->left = !this->left;
-                        ant->left = !ant->left;
-                }
-                bool compare(Ant *ant) {
-                        if(!this->left && ant->left) {
-                                this->reverse(ant);
-                                return true;
-                        }
-                        return false;
-                }
+struct Ant {
+        int dir;
+        int start;
 };
 
-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) {
-                for(i=0; i<ants; i++) {
-                        scanf("%d %s", &pos, dir);
-                        ant[i].id = pos;
-                        ant[i].left = (dir[0]=='L');
-                        ant[i].time = (ant[i].left ? pos : len-pos);
+int main()
+{
+        int ants, pos, woodlen;
+        char dir;
+        while(scanf("%d %d", &woodlen, &ants)>0) 
+        {
+                Ant *wood[woodlen];
+                memset (wood,NULL,woodlen*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 );
                 }
-                std::sort(ant+0, ant+ants);
-                for(i=0; i<ants-1; i++) {
-                        j=i;
-                        while(ant[j].compare(ant+j+1) && j>0) {
-                                if(j==0) {
-                                        break;
+                                
+                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++;
+                                        }
                                 }
-                                j--;
                         }
+                        
+                        time++;
+                        
                 }
                 
-                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(Rf!=NULL && Lf!=NULL)
+                {
+                        int a = Rf->start,b = Lf->start;
+                        
+                        if(Rf->start > Lf->start)
+                        {
+                                a=Lf->start;
+                                b=Rf->start;    
                         }
+                
+                        printf("The last will fall down in %d seconds - started at %d and %d.\n", time,a,b);
+                
                 }
-                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);
+                
+                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);
+                        }
                 }
+        
         }
         return 0;