Source code for submission s1344

ants.cpp

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #define MAX 100010
  4. int l,n;
  5. char dirs[MAX];
  6. int ants[MAX];
  7. int start,end;
  8. int lcnt;
  9. int maxL,maxR;
  10. int pos2,pos1;
  11.  
  12. int getLastL(){
  13. int ctr=0;
  14. for(int i=0;i<=l;i++){
  15. if(ants[i]!=-1)
  16. ctr++;
  17. if(ctr == lcnt)
  18. return i;
  19. }
  20. return n+100;
  21. }
  22. int getLastR(){
  23. int ctr=0;
  24. for(int i=0;i<=l;i++){
  25. if(ants[i]!=-1)
  26. ctr++;
  27. if(ctr == lcnt+1)
  28. return i;
  29. }
  30. return n+100;
  31. }
  32.  
  33. int main(int argc, char ** argv){
  34. int pos;
  35. char dir;
  36. bool noL,noR;
  37. int pom;
  38.  
  39. while(scanf("%d %d",&l,&n) == 2){
  40. noL = noR = true;
  41. lcnt=0;
  42. maxR = maxL = 0;
  43. pos = 0;
  44. for(int i=0; i <= l;i++){
  45. ants[i] = -1;
  46. }
  47. for(int i=0; i < n;i++){
  48. scanf("%d %c",&pos,&dir);
  49. if(dir == 'R'){
  50. noR = false;
  51. ants[pos] = l - pos;
  52. if(ants[maxL] < ants[pos]){
  53. maxL = pos;
  54. }
  55. }
  56. else{
  57. lcnt++;
  58. noL = false;
  59. ants[pos] = pos;
  60. if(ants[maxR] < ants[pos]){
  61. maxR = pos;
  62. }
  63. }
  64. }
  65. // no left oriented
  66. if(noL){
  67. printf("The last ant will fall down in %d seconds - started at %d.\n",ants[maxL],maxL);
  68.  
  69.  
  70. continue;
  71. }
  72. // no right oriented
  73. if(noR){
  74. printf("The last ant will fall down in %d seconds - started at %d.\n",ants[maxR],maxR);
  75.  
  76. continue;
  77. }
  78. //printf("%d %d \n",maxL,maxR);
  79. // for(int i=0;i<=l;i++){
  80. //if(ants[i]!=-1)
  81. // printf("%d ",i);
  82. //printf("\n");
  83. // }
  84.  
  85.  
  86.  
  87. if(ants[maxL] > ants[maxR]){
  88. pos1 = getLastL();
  89. printf("The last ant will fall down in %d seconds - started at %d.\n",ants[maxL],pos1);
  90. continue;
  91. }
  92. if(ants[maxR] > ants[maxL]){
  93. pos1 = getLastR();
  94. printf("The last ant will fall down in %d seconds - started at %d.\n",ants[maxR],pos1);
  95. continue;
  96. }
  97. pos1 = getLastR();
  98. pos2 = getLastL();
  99. if(pos2 < pos1){
  100. pom = pos1;
  101. pos1 = pos2;
  102. pos2 = pom;
  103. }
  104. printf("The last ant will fall down in %d seconds - started at %d and %d.\n",ants[maxR],pos1,pos2);
  105.  
  106. }
  107. return 0;
  108. }
  109.