Source code for submission s1278

Go to diff to previous submission

ants.cpp

  1. #include <limits.h>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. int getLen(int length, int pos, char dir) {
  9. int max;
  10. if (dir=='L') {
  11. max=pos;
  12. } else {
  13. max = length-pos;
  14. }
  15. return max;
  16. }
  17.  
  18. char ants[100001];
  19.  
  20. int main(){
  21. int length;
  22. int antsCount;
  23. int prevCount = 0;
  24.  
  25. for (int i=0; i<100001; i++) {
  26. ants[i] = '0';
  27. }
  28.  
  29. while (scanf("%d %d ", &length, &antsCount) == 2) {
  30.  
  31. for (int i=0; i<100001; i++) {
  32. ants[i] = '0';
  33. }
  34. prevCount = antsCount;
  35.  
  36. int maxCount=0;
  37. int maxPos[4];
  38. int max = 0;
  39.  
  40. for (int curAnts=0; curAnts < antsCount; curAnts++) {
  41. int pos;
  42. char dir;
  43.  
  44. scanf("%d %c ", &pos, &dir);
  45.  
  46. ants[pos] = dir;
  47.  
  48. int curLen = getLen(length, pos, dir);
  49. if (curLen > max) {
  50. maxPos[0] = pos;
  51. maxCount = 1;
  52. max = curLen;
  53. } else if (curLen == max) {
  54. maxPos[maxCount++] = pos;
  55. }
  56. }
  57.  
  58. int prevPos=-1;
  59. for (int i=0; i<maxCount; i++) {
  60. char myDir = ants[maxPos[i]];
  61. int myPos = maxPos[i];
  62.  
  63. for (int j=maxPos[i]; j<100001 && j>=0;) {
  64. if (myDir == 'L') {
  65. if (ants[j] == 'R' || ants[j] == 'L') {
  66. if (myPos != j) {
  67. maxPos[i] = j;
  68. prevPos = j;
  69. if (ants[j] == 'L') {
  70. break;
  71. }
  72. } else {
  73. prevPos = j;
  74. }
  75. }
  76. j--;
  77. } else {
  78. if (ants[j] == 'R' || ants[j] == 'L') {
  79. if (myPos != j) {
  80. maxPos[i] = j;
  81. prevPos = j;
  82. if (ants[j] == 'R') {
  83. break;
  84. }
  85. } else {
  86. prevPos = j;
  87. }
  88. }
  89. j++;
  90. }
  91. }
  92. }
  93.  
  94. printf("The last ant will fall down in %d seconds - started at ", max);
  95. if (maxCount == 1) {
  96. printf("%d.\n", maxPos[0]);
  97. } else {
  98. if (maxPos[0] > maxPos[1]) {
  99. printf("%d and %d.\n", maxPos[1], maxPos[0]);
  100. } else {
  101. printf("%d and %d.\n", maxPos[0], maxPos[1]);
  102. }
  103. }
  104.  
  105.  
  106.  
  107.  
  108. }
  109.  
  110.  
  111. }
  112.  
  113.  

Diff to submission s1254

ants.cpp

--- c4.s1254.cteam027.ants.cpp.0.ants.cpp
+++ c4.s1278.cteam027.ants.cpp.0.ants.cpp
@@ -56,4 +56,5 @@
                         }
                         
+                        int prevPos=-1;
                         for (int i=0; i<maxCount; i++) {
                                 char myDir = ants[maxPos[i]];
@@ -62,20 +63,26 @@
                                 for (int j=maxPos[i]; j<100001 && j>=0;) {
                                         if (myDir == 'L') {
-                                                if (myPos != j) {
-                                                        if (ants[j] == 'R') {
-                                                                maxPos[i] = j;
-                                                        } else if (ants[j] == 'L') {
+                                                if (ants[j] == 'R' || ants[j] == 'L') {
+                                                        if (myPos != j) {
                                                                 maxPos[i] = j;
-                                                                break;
+                                                                prevPos = j;
+                                                                if (ants[j] == 'L') {
+                                                                        break;
+                                                                }
+                                                        } else {
+                                                                prevPos = j;
                                                         }
                                                 }
                                                 j--;
                                         } else {
-                                                if (myPos != j) {
-                                                        if (ants[j] == 'L') {
-                                                                maxPos[i] = j;
-                                                        } else if (ants[j] == 'R') {
+                                                if (ants[j] == 'R' || ants[j] == 'L') {
+                                                        if (myPos != j) {
                                                                 maxPos[i] = j;
-                                                                break;
+                                                                prevPos = j;
+                                                                if (ants[j] == 'R') {
+                                                                        break;
+                                                                }
+                                                        } else {
+                                                                prevPos = j;
                                                         }
                                                 }