Source code for submission s1005

ants.cpp

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