Source code for submission s1267

ants.cpp

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. int pole[1000000];
  6. int compare_ints(const void* a, const void* b)
  7. {
  8. const int *arg1 = (int*)a;
  9. const int *arg2 = (int*)b;
  10.  
  11. return *arg1 - *arg2;
  12. }
  13.  
  14. int main()
  15. {
  16. int l,a;
  17. while(scanf("%i %i",&l,&a)==2)
  18. {
  19. int p;
  20. char d;
  21. int m = 0;
  22. bool dva = false;
  23. int poctl = 0;
  24. char mc = '0';
  25.  
  26. for(int i = 0; i < a; ++i)
  27. {
  28. scanf("%i %c",&p, &d);
  29. pole[i] = p;
  30.  
  31.  
  32. if (d == 'L' )
  33. {
  34. poctl ++;
  35. if (p == m && mc == 'R')
  36. dva = true;
  37.  
  38. if (p > m)
  39. {
  40. m = p;
  41. dva =false;
  42. mc = 'L';
  43. }
  44. }
  45. else
  46. {
  47. if (l-p == m && mc == 'L')
  48. dva = true;
  49.  
  50. if (l-p > m)
  51. {
  52. m = l-p;
  53. dva = false;
  54. mc = 'R';
  55. }
  56.  
  57. }
  58.  
  59.  
  60. }
  61.  
  62.  
  63. qsort(pole, a, sizeof(int), compare_ints);
  64.  
  65. if (dva)
  66. {
  67. printf("The last ant will fall down in %i seconds - started at %i and %i.\n", m, pole[poctl-1], pole[poctl]);
  68. }
  69. else
  70. {
  71. printf("The last ant will fall down in %i seconds - started at %i.\n", m, pole[poctl]);
  72. }
  73.  
  74.  
  75. }
  76. return 0;
  77. }
  78.