Source code for submission s829

ants.cpp

  1. #include <vector>
  2. #include <algorithm>
  3. #include <cstdio>
  4. using namespace std;
  5.  
  6. int main() {
  7. int l, a;
  8. while(scanf("%d %d", &l, &a) == 2) {
  9. vector<int> pos(a);
  10. vector<int> left;
  11. vector<int> right;
  12. for (int i = 0; i < a; ++i) {
  13. char c;
  14. scanf("%d %c", &pos[i], &c);
  15. if (c == 'L')
  16. left.push_back(pos[i]);
  17. else
  18. right.push_back(l - pos[i]);
  19. }
  20. sort(pos.begin(), pos.end());
  21. sort(left.begin(), left.end());
  22. sort(right.begin(), right.end());
  23. int i1 = left.size() - 1;
  24. int i2 = i1 + 1;
  25. if (i1 >= 0 && i2 < a && left.back() == right.back()) {
  26. printf("The last ant will fall down in %d seconds - started at %d and %d.\n", left.back(), pos[i1], pos[i2]);
  27. } else {
  28. int tlast, xlast;
  29. if (i1 < 0 || (i2 < a && left.back() < right.back())) {
  30. tlast = right.back();
  31. xlast = pos[i2];
  32. } else {
  33. tlast = left.back();
  34. xlast = pos[i1];
  35. }
  36. printf("The last ant will fall down in %d seconds - started at %d.\n", tlast, xlast);
  37. }
  38.  
  39. }
  40. }
  41.