Source code for submission s1254

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. for (int i=0; i<maxCount; i++) {
  59. char myDir = ants[maxPos[i]];
  60. int myPos = maxPos[i];
  61.  
  62. for (int j=maxPos[i]; j<100001 && j>=0;) {
  63. if (myDir == 'L') {
  64. if (myPos != j) {
  65. if (ants[j] == 'R') {
  66. maxPos[i] = j;
  67. } else if (ants[j] == 'L') {
  68. maxPos[i] = j;
  69. break;
  70. }
  71. }
  72. j--;
  73. } else {
  74. if (myPos != j) {
  75. if (ants[j] == 'L') {
  76. maxPos[i] = j;
  77. } else if (ants[j] == 'R') {
  78. maxPos[i] = j;
  79. break;
  80. }
  81. }
  82. j++;
  83. }
  84. }
  85. }
  86.  
  87. printf("The last ant will fall down in %d seconds - started at ", max);
  88. if (maxCount == 1) {
  89. printf("%d.\n", maxPos[0]);
  90. } else {
  91. if (maxPos[0] > maxPos[1]) {
  92. printf("%d and %d.\n", maxPos[1], maxPos[0]);
  93. } else {
  94. printf("%d and %d.\n", maxPos[0], maxPos[1]);
  95. }
  96. }
  97.  
  98.  
  99.  
  100.  
  101. }
  102.  
  103.  
  104. }
  105.  
  106.  

Diff to submission s1196

ants.cpp

--- c4.s1196.cteam027.ants.cpp.0.ants.cpp
+++ c4.s1254.cteam027.ants.cpp.0.ants.cpp
@@ -66,4 +66,5 @@
                                                                 maxPos[i] = j;
                                                         } else if (ants[j] == 'L') {
+                                                                maxPos[i] = j;
                                                                 break;
                                                         }
@@ -75,4 +76,5 @@
                                                                 maxPos[i] = j;
                                                         } else if (ants[j] == 'R') {
+                                                                maxPos[i] = j;
                                                                 break;
                                                         }