Source code for submission s823

Go to diff to previous submission

ants.cpp

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

Diff to submission s816

ants.cpp

--- c4.s816.cteam102.ants.cpp.0.ants.cpp
+++ c4.s823.cteam102.ants.cpp.0.ants.cpp
@@ -36,5 +36,5 @@
 int main() {
         Ant ant[100000];
-        int ants, pos, len, i;
+        int ants, pos, len, i, j;
         int maxTime=-1, maxAnt1=-1, maxAnt2=-1;
         char dir[3];
@@ -45,22 +45,13 @@
                         ant[i].left = (dir[0]=='L');
                         ant[i].time = (ant[i].left ? pos : len-pos);
-                        if(maxTime==ant[i].time) {
-                                maxAnt2=pos;
-                        }else if (maxTime<ant[i].time) {
-                                maxTime = ant[i].time;
-                                maxAnt1=pos;
-                                maxAnt2=-1;
-                        }
                 }
                 std::sort(ant+0, ant+ants);
-                for(i=0; i<ants-1;) {
-                        if(ant[i].compare(ant+i+1) && i>0) {
-                                if(i>0) {
-                                        i--;
-                                } else {
-                                        i++;
+                for(i=0; i<ants-1; i++) {
+                        j=i;
+                        while(ant[j].compare(ant+j+1) && j>0) {
+                                if(j==0) {
+                                        break;
                                 }
-                        }else {
-                                i++;
+                                j--;
                         }
                 }