Source code for submission s1196

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

Diff to submission s1189

ants.cpp

--- c4.s1189.cteam027.ants.cpp.0.ants.cpp
+++ c4.s1196.cteam027.ants.cpp.0.ants.cpp
@@ -60,5 +60,5 @@
                                 int  myPos = maxPos[i];
                                 
-                                for (int j=maxPos[i]; j<100000 && j>=0;) {
+                                for (int j=maxPos[i]; j<100001 && j>=0;) {
                                         if (myDir == 'L') {
                                                 if (myPos != j) {