Source code for submission s1262

Go to diff to previous submission

ants.cpp

  1. #include <cstdio>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. int main(void) {
  7. int count, length;
  8.  
  9. while(scanf("%d%d\n", &length, &count) == 2) {
  10. int leftmost = -1, rightmost = -1;
  11. bool ants[100000];
  12. for(int i = 0; i < 100000; ++i) ants[i] = false;
  13.  
  14. int pos;
  15. char dir;
  16.  
  17. int toLeft = 0;
  18.  
  19. for(int a = 0; a < count; ++a) {
  20. scanf("%d %c\n", &pos, &dir);
  21. ants[pos] = true;
  22. if(dir == 'R' && (leftmost == -1 || pos < leftmost)) {
  23. leftmost = pos;
  24. }
  25. else if(dir == 'L' && (rightmost == -1 || pos > rightmost)) {
  26. rightmost = pos;
  27. }
  28.  
  29. if(dir == 'L') ++toLeft;
  30. }
  31.  
  32. int maxTime = 0;
  33. if(leftmost != -1) maxTime = length - leftmost;
  34. if(rightmost != -1 && rightmost > maxTime) maxTime = rightmost;
  35.  
  36. printf("The last ant will fall down in %d seconds - started at ", maxTime);
  37.  
  38. if(rightmost == length - leftmost) {
  39. int passed = 0;
  40. for(int a = 0; a <= length; ++a) {
  41. if(!ants[a]) continue;
  42. ++passed;
  43.  
  44. if(passed == toLeft) {
  45. printf("%d and ", a);
  46. }
  47. else if(passed == toLeft + 1) {
  48. printf("%d", a);
  49. break;
  50. }
  51. }
  52. }
  53. else if(maxTime == length - leftmost) {
  54. int passed = 0;
  55. for(int a = 0; a <= length; ++a) {
  56. if(!ants[a]) continue;
  57. if(++passed == toLeft + 1) {
  58. printf("%d", a);
  59. break;
  60. }
  61. }
  62. }
  63. else {
  64. int passed = 0;
  65. for(int a = 0; a <= length; ++a) {
  66. if(!ants[a]) continue;
  67. if(++passed == toLeft) {
  68. printf("%d", a);
  69. break;
  70. }
  71. }
  72. }
  73.  
  74. printf(".\n");
  75. }
  76.  
  77. return 0;
  78. }
  79.  

Diff to submission s1241

ants.cpp

--- c4.s1241.cteam025.ants.cpp.0.ants.cpp
+++ c4.s1262.cteam025.ants.cpp.0.ants.cpp
@@ -38,5 +38,5 @@
                 if(rightmost == length - leftmost) {
                         int passed = 0;
-                        for(int a = 0; a < length; ++a) {
+                        for(int a = 0; a <= length; ++a) {
                                 if(!ants[a]) continue;
                                 ++passed;
@@ -53,5 +53,5 @@
                 else if(maxTime == length - leftmost) {
                         int passed = 0;
-                        for(int a = 0; a < length; ++a) {
+                        for(int a = 0; a <= length; ++a) {
                                 if(!ants[a]) continue;
                                 if(++passed == toLeft + 1) {
@@ -63,5 +63,5 @@
                 else {
                         int passed = 0;
-                        for(int a = 0; a < length; ++a) {
+                        for(int a = 0; a <= length; ++a) {
                                 if(!ants[a]) continue;
                                 if(++passed == toLeft) {