Source code for submission s1070

Go to diff to previous submission

ants.cpp

  1. #include <cstdio>
  2.  
  3. int main(){
  4. int len, ants, cnt, maxCesta, maxIndex, maxIndex2, help;
  5. char dir;
  6. bool dva;
  7. while(1){
  8. maxCesta = cnt = maxIndex = maxIndex2 = help = 0;
  9. dva = false;
  10. if(scanf("%d %d\n", &len, &ants) != 2) return 0;
  11. int pole[len + 1];
  12. for(int i = 0; i < len; i++) pole[i] = 0;
  13. for(int i = 0; i < ants; i++){
  14. scanf("%d %c\n", &help, &dir);
  15. dir == 'R' ? pole[help] = 1 : pole[help] = -1;
  16.  
  17. if((maxCesta == (len - help)) && (dir == 'R')){
  18. maxIndex2 = help;
  19. dva = true;
  20. }
  21. if((maxCesta < (len - help)) && (dir =='R')){
  22. maxCesta = len - help;
  23. maxIndex = help;
  24. dva = false;
  25. }
  26. if((maxCesta == help) && (dir == 'l')){
  27. maxIndex2 = help;
  28. dva = true;
  29. }
  30. if((maxCesta < help) && (dir == 'L')){
  31. maxCesta = maxIndex = help;
  32. dva = false;
  33. }
  34. }
  35. switch(pole[maxIndex]){
  36. case -1:{
  37. for(int i = maxIndex; i >=0; i--){
  38. if(pole[i] == 1) cnt++;
  39. }
  40. for(int i = maxIndex; i >=0 ; i--){
  41. if(pole[i] != 0){
  42. if(cnt == 0) maxIndex = i;
  43. cnt--;
  44. }
  45. }
  46. break;
  47. }
  48. case 1:{
  49. for(int i = maxIndex; i <= len; i++){
  50. if(pole[i] == -1) cnt++;
  51. }
  52. for(int i = maxIndex; i <= len ; i++){
  53. if(pole[i] != 0){
  54. if(cnt == 0) maxIndex = i;
  55. cnt--;
  56. }
  57. }
  58. break;
  59. }
  60. default:{break;}
  61. }
  62.  
  63. if(dva){
  64. cnt = 0;
  65. switch(pole[maxIndex2]){
  66. case -1:{
  67. for(int i = maxIndex2; i >=0; i--){
  68. if(pole[i] == 1) cnt++;
  69. }
  70. for(int i = maxIndex2; i >=0 ; i--){
  71. if(pole[i] != 0){
  72. if(cnt == 0) maxIndex2 = i;
  73. cnt--;
  74. }
  75. }
  76. break;
  77. }
  78. case 1:{
  79. for(int i = maxIndex2; i <= len; i++){
  80. if(pole[i] == -1) cnt++;
  81. }
  82. for(int i = maxIndex2; i <= len ; i++){
  83. if(pole[i] != 0){
  84. if(cnt == 0) maxIndex2 = i;
  85. cnt--;
  86. }
  87. }
  88. break;
  89. }
  90. default:{break;}
  91. }}
  92. if(dva) printf("The last ant will fall down in %d seconds - started at %d and %d.\n", maxCesta, maxIndex < maxIndex2 ? maxIndex : maxIndex2, maxIndex < maxIndex2 ? maxIndex2 : maxIndex);
  93. else
  94. printf("The last ant will fall down in %d seconds - started at %d.\n", maxCesta, maxIndex);
  95. }
  96. return 0;
  97. }
  98.  
  99.  

Diff to submission s1005

ants.cpp

--- c4.s1005.cteam037.ants.cpp.0.ants.cpp
+++ c4.s1070.cteam037.ants.cpp.0.ants.cpp
@@ -2,16 +2,13 @@
 
 int main(){
-        int len, ants;
+        int len, ants, cnt, maxCesta, maxIndex, maxIndex2, help;
+        char dir;
+        bool dva;
         while(1){
+                maxCesta = cnt = maxIndex = maxIndex2 = help = 0;
+                dva = false;
                 if(scanf("%d %d\n", &len, &ants) != 2) return 0;
-                int pole[len];
+                int pole[len + 1];
                 for(int i = 0; i < len; i++) pole[i] = 0;
-                int maxCesta = 0;
-                int maxIndex = 0;
-                int maxIndex2 = 0;
-                int maxCesta2 = 0;
-                int help = 0;
-                char dir;
-                bool dva = false;
                 for(int i = 0; i < ants; i++){
                         scanf("%d %c\n", &help, &dir);
@@ -19,5 +16,4 @@
 
                         if((maxCesta == (len - help)) && (dir == 'R')){
-                                maxCesta2 = len - help;
                                 maxIndex2 = help;
                                 dva = true;
@@ -27,8 +23,7 @@
                                 maxIndex = help;
                                 dva = false;
-                                //printf("MINDEX:%d\n", maxIndex);
                         }
                         if((maxCesta == help) && (dir == 'l')){
-                                maxCesta2 = maxIndex2 = help;
+                                maxIndex2 = help;
                                 dva = true;
                         }
@@ -36,8 +31,6 @@
                                 maxCesta = maxIndex = help;
                                 dva = false;
-                                //printf("MINDEX:%d\n", maxIndex);
                         }
                 }       
-                int cnt = 0;
                 switch(pole[maxIndex]){
                         case -1:{
@@ -45,5 +38,4 @@
                                         if(pole[i] == 1) cnt++;
                                 }
-                                //printf("CNTa:%d\n", cnt);
                                 for(int i = maxIndex; i >=0 ; i--){
                                         if(pole[i] != 0){
@@ -55,9 +47,8 @@
                         }
                         case 1:{
-                                for(int i = maxIndex; i < len; i++){
+                                for(int i = maxIndex; i <= len; i++){
                                         if(pole[i] == -1) cnt++;
                                 }
-                                //printf("CNTb:%d\n", cnt);
-                                for(int i = maxIndex; i < len ; i++){
+                                for(int i = maxIndex; i <= len ; i++){
                                         if(pole[i] != 0){
                                                 if(cnt == 0) maxIndex = i;
@@ -67,5 +58,5 @@
                                 break;
                         }
-                        default:{printf("FUCK\n");break;}
+                        default:{break;}
                 }
 
@@ -77,5 +68,4 @@
                                         if(pole[i] == 1) cnt++;
                                 }
-                                //printf("CNTa:%d\n", cnt);
                                 for(int i = maxIndex2; i >=0 ; i--){
                                         if(pole[i] != 0){
@@ -87,9 +77,8 @@
                         }
                         case 1:{
-                                for(int i = maxIndex2; i < len; i++){
+                                for(int i = maxIndex2; i <= len; i++){
                                         if(pole[i] == -1) cnt++;
                                 }
-                                //printf("CNTb:%d\n", cnt);
-                                for(int i = maxIndex2; i < len ; i++){
+                                for(int i = maxIndex2; i <= len ; i++){
                                         if(pole[i] != 0){
                                                 if(cnt == 0) maxIndex2 = i;
@@ -99,5 +88,5 @@
                                 break;
                         }
-                        default:{printf("FUCK\n");break;}
+                        default:{break;}
                 }}
                 if(dva) printf("The last ant will fall down in %d seconds - started at %d and %d.\n", maxCesta, maxIndex < maxIndex2 ? maxIndex : maxIndex2, maxIndex < maxIndex2 ? maxIndex2 : maxIndex);