Source code for submission s972

ants.cpp

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <ctype.h>
  6. #include <math.h>
  7.  
  8. #include <string>
  9. #include <vector>
  10. #include <deque>
  11. #include <map>
  12. #include <set>
  13. #include <algorithm>
  14.  
  15. using namespace std;
  16.  
  17. typedef pair <int, int> PII;
  18. typedef long long int LL;
  19. typedef vector <int> VI;
  20. typedef vector <LL> VLL;
  21.  
  22. #define FOR(i, a, b) for ( int i = a; i < b; ++i )
  23. #define FORD(i, a, b) for ( int i = a-1; i >= 0; --i )
  24.  
  25. #define FILL(x, v, n) for ( int _i = 0; _i < n; ++_i ) x[_i] = v;
  26.  
  27. vector< pair< int, bool> > drevo;
  28.  
  29. bool comp(pair<int, bool> a, pair<int, bool> b)
  30. {
  31. return a.first < b.first;
  32. }
  33.  
  34. int last (int pos, bool smer){ //smer, ktory ma posledny mravec a index v drevo, na ktorom sa nachadza
  35.  
  36. if ( pos == -1 )
  37. return -1;
  38.  
  39. deque<int> D;
  40.  
  41. for (unsigned int k = 0; k < drevo.size(); k++)
  42. {
  43. int i;
  44.  
  45. if (smer)
  46. i = k;
  47. else
  48. i = drevo.size() - 1 - k;
  49.  
  50. if ( drevo[i].first == pos ) //skoncil som
  51. {
  52. if ( D.empty() )
  53. return drevo[i].first;
  54. else
  55. return D.front();
  56. }
  57.  
  58. if ( drevo[i].second != smer)
  59. {
  60. D.push_back(drevo[i].first);
  61. }
  62. else
  63. {
  64. if ( D.empty() )
  65. continue;
  66. else
  67. {
  68. D.pop_front();
  69. D.push_back(drevo[i].first);
  70. }
  71. }
  72. }
  73.  
  74. abort();
  75. }
  76.  
  77. int main() {
  78. int L, A;
  79.  
  80. while (scanf("%d %d", &L, &A) == 2)
  81. {
  82. drevo.clear();
  83.  
  84. int max = -1;
  85. int i1, i2 = -1;
  86. bool smer1, smer2 = false;
  87.  
  88. FOR(i,0,A)
  89. {
  90. int p;
  91. char c;
  92. scanf("%d %c", &p, &c);
  93.  
  94. int d;
  95. bool smer;
  96.  
  97. if ( c == 'R' )
  98. {
  99. smer = false;
  100. d = L-p;
  101. }
  102. else if ( c == 'L' )
  103. {
  104. smer = true;
  105. d = p;
  106. }
  107.  
  108. if ( d > max )
  109. {
  110. max = d;
  111. i1 = p;
  112. smer1 = smer;
  113. i2 = -1;
  114. }
  115. else if ( d == max )
  116. {
  117. i2 = p;
  118. smer2 = smer;
  119. }
  120.  
  121.  
  122. drevo.push_back(make_pair(p,smer));
  123. }
  124.  
  125. sort(drevo.begin(), drevo.end(), comp);
  126.  
  127. int p1 = last(i1, smer1);
  128. int p2 = last(i2, smer2);
  129.  
  130. printf("The last ant will fall down in %d seconds - started at ", max);
  131.  
  132. if ( p2 == -1 )
  133. {
  134. printf("%d.\n",p1);
  135. }
  136. else
  137. printf("%d and %d.\n",min(p1,p2), std::max(p1,p2));
  138. }
  139.  
  140. return 0;
  141.  
  142. }
  143.