Source code for submission s901

ants.cpp

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <climits>
  6.  
  7. #include <iostream>
  8. #include <algorithm>
  9.  
  10. #include <vector>
  11. #include <string>
  12. #include <map>
  13. #include <queue>
  14. #include <deque>
  15. #include <stack>
  16. #include <set>
  17.  
  18. //~ using namespace std;
  19.  
  20. typedef long double num;
  21. typedef long long int lint;
  22. typedef struct { int x, y; } intpoint;
  23. typedef struct { num x, y; } numpoint;
  24. typedef struct { lint x, y; } lintpoint;
  25.  
  26. #define EPS (1e-7L)
  27. #define PI (4.0L * atanl(1.0L))
  28.  
  29. #define min(a, b) ((a) < (b) ? (a) : (b))
  30. #define max(a, b) ((a) > (b) ? (a) : (b))
  31. #define abs(a) ((a) < 0 ? -(a) : (a))
  32.  
  33. int main(int argc, char *argv[]) {
  34. while (true) {
  35. int ants, len;
  36.  
  37. bool existL = false, existR = false, collision;
  38.  
  39. if (2 != scanf("%u %u\n", &len, &ants))
  40. break;
  41. int maxL = 0, minR = len, mindist = len;
  42. std::vector<int> antssssss;
  43.  
  44.  
  45.  
  46. bool hasR = false, hasL = false;
  47. int startR = INT_MAX, startL = 0;
  48.  
  49. for (int ant = 0; ant < ants; ant++) {
  50. int start;
  51. char dir;
  52.  
  53. scanf("%u %c\n", &start, &dir);
  54.  
  55. if (dir == 'L' && startL <= start) {
  56. existL = true;
  57. maxL = max(maxL, start);
  58.  
  59. hasL = true;
  60. startL = start;
  61. } else if (dir == 'R' && start <= startR) {
  62. existR = true;
  63. minR = min(minR, start);
  64.  
  65. hasR = true;
  66. startR = start;
  67. }
  68. int aux = len%2 ? min(abs(len/2 - start), abs(len/2 + 1 - start)) : abs((len >>1) - start);
  69. if (aux < mindist)
  70. {
  71. antssssss.clear();
  72. antssssss.push_back(start);
  73. mindist = aux;
  74. }
  75. else
  76. if(aux == mindist)
  77. antssssss.push_back(start);
  78.  
  79. }
  80.  
  81.  
  82.  
  83. int L = startL, R = len - startR;
  84. if (existL && existR) {
  85. if (minR < maxL) {
  86. if(antssssss.size() == 2)
  87. printf("The last ant will fall down in %d seconds - started at %d and %d.\n", max(L, R), antssssss[0], antssssss[1]);
  88. else
  89. printf("The last ant will fall down in %d seconds - started at %d.\n", max(L, R), antssssss[0]);
  90. }
  91. } else if (existR) {
  92. printf("The last ant will fall down in %d seconds - started at %d.\n", R, startR);
  93. } else if (existL) {
  94. printf("The last ant will fall down in %d seconds - started at %d.\n", L, startL);
  95. } else {
  96. // not gonna happen
  97. }
  98. }
  99.  
  100. return 0;
  101. }
  102.