Source code for submission s1233

ants.cpp

  1. #include <stdio.h>
  2. #include <algorithm>
  3.  
  4. struct Ant{
  5. int pos;
  6. char dir;
  7. };
  8.  
  9. bool comparer(Ant const & a, Ant const & b)
  10. {
  11. return a.pos<b.pos;
  12. }
  13.  
  14. int main() {
  15.  
  16. Ant * ants(0);
  17. int L;
  18. int A;
  19.  
  20. // main cycle
  21. while( scanf( "%d %d\n", &L, &A ) == 2 ) {
  22. delete[] ants;
  23. ants = new Ant[A];
  24.  
  25. int leftValueStart(-1);
  26. int rightValueStart(-1);
  27. int leftValueBest(-1);
  28. int rightValueBest(-1);
  29.  
  30. // read ants
  31. Ant * afterEnd(ants+A);
  32. for( Ant * cur(ants); cur < afterEnd; ++cur )
  33. scanf( "%d %c\n", &(cur->pos), &(cur->dir) );
  34.  
  35. // sort array
  36. std::sort(ants,ants+A,comparer);
  37.  
  38. // find leftWalkerBest
  39. int opositeWalkers(0);
  40. for( Ant * cur(ants); cur < afterEnd; ++cur ) {
  41. if( cur->dir == 'R' )
  42. ++opositeWalkers;
  43.  
  44. else
  45. {
  46. leftValueBest = cur->pos;
  47. leftValueStart = (cur-opositeWalkers)->pos;
  48.  
  49. }
  50. }
  51.  
  52.  
  53. // find rightWalkerBest
  54. opositeWalkers = 0;
  55. for( Ant * cur(ants+A-1); cur >= ants ; --cur ) {
  56. //printf( "\n\n%d %c\n", (cur->pos), (cur->dir) );
  57. if( cur->dir == 'L' )
  58. {
  59. ++opositeWalkers;
  60. //printf("\nOV%d",opositeWalkers);
  61. }
  62. else
  63. {
  64. rightValueBest = L - cur->pos;
  65. rightValueStart = (cur+opositeWalkers)->pos;
  66. //printf("\nCHG%d %d",leftValueBest,leftValueStart);
  67. }
  68. }
  69.  
  70. if( leftValueBest < rightValueBest ) {
  71. printf( "The last ant will fall down in %d seconds - started at %d\n", rightValueBest, rightValueStart );
  72. } else if ( leftValueBest > rightValueBest ) {
  73. printf( "The last ant will fall down in %d seconds - started at %d\n", leftValueBest, leftValueStart );
  74. } else {
  75. printf( "The last ant will fall down in %d seconds - started at %d and %d\n", leftValueBest, leftValueStart, rightValueStart );
  76. }
  77. }
  78.  
  79. return 0;
  80. }
  81.