Source code for submission s956

ants.cpp

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <climits>
  4.  
  5. using namespace std;
  6.  
  7. int main(){
  8.  
  9. int length, ants, minR=INT_MAX, maxL=INT_MIN, pos, timeR, size=100000, lenR=0, lenL=0, medL, medR;
  10. char dir;
  11. int left[size];
  12. int right[size];
  13. bool wasRight=false, wasLeft=false;
  14.  
  15. while(cin>> length >> ants)
  16. {
  17. for(int i=0; i<ants; i++)
  18. {
  19. cin >> pos >> dir;
  20. if(dir=='R')
  21. {
  22. wasRight=true;
  23. if(pos<minR) minR=pos;
  24. right[lenR]=pos;
  25. lenR++;
  26. }
  27. else
  28. {
  29. wasLeft=true;
  30. if(pos>maxL) maxL=pos;
  31. left[lenL]=pos;
  32. lenL++;
  33. }
  34. }
  35. timeR=length-minR; //cas
  36.  
  37. if(wasLeft && !wasRight)
  38. {
  39. cout << "The last ant will fall down in " << maxL << " seconds - started at " << maxL << endl;
  40. }
  41. else if(!wasLeft && wasRight)
  42. {
  43. cout << "The last ant will fall down in " << timeR << " seconds - started at " << minR << endl;
  44. }
  45. else
  46. {
  47. if(timeR<maxL)
  48. {
  49. //cout << maxL << " " << minR << endl;
  50. sort(&left[0], &left[lenL]);
  51. medL=lenL/2;
  52.  
  53. cout << "The last ant will fall down in " << maxL << " seconds - started at " << left[medL] << endl;
  54.  
  55. //cout << "The last ant will fall down in " << maxL << " seconds - started at " << maxL << endl;
  56. }
  57. else if(timeR>maxL)
  58. {
  59. //cout << maxL << " " << minR << endl;
  60. sort(&right[0], &right[lenR]);
  61. medR=lenR/2;
  62. cout << "The last ant will fall down in " << minR << " seconds - started at " << right[medR] << endl;
  63. }
  64. else if(timeR==maxL)
  65. {
  66. //cout << maxL << " " << timeR << endl;
  67. sort(&left[0], &left[lenL]);
  68. sort(&right[0], &right[lenR]);
  69. medR=lenR/2;
  70. medL=lenL/2;
  71. cout << "The last ant will fall down in " << maxL << " seconds - started at " << left[medL] << " and " << right[medR] << endl;
  72. }
  73. }
  74. lenL = lenR = 0;
  75. minR=INT_MAX; maxL=INT_MIN;
  76. wasLeft = wasRight = false;
  77.  
  78. }
  79.  
  80.  
  81. return 0;
  82. }
  83.