ants.cpp
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
int main() {
int l, a;
while(scanf("%d %d", &l, &a) == 2) {
vector<int> pos(a);
vector<int> left;
vector<int> right;
for (int i = 0; i < a; ++i) {
char c;
scanf("%d %c", &pos[i], &c);
if (c == 'L')
left.push_back(pos[i]);
else
right.push_back(l - pos[i]);
}
sort(pos.begin(), pos.end());
sort(left.begin(), left.end());
sort(right.begin(), right.end());
int i1 = left.size() - 1;
int i2 = i1 + 1;
if (i1 >= 0 && i2 < a && left.back() == right.back()) {
printf("The last ant will fall down in %d seconds - started at %d and %d.\n", left.back(), pos[i1], pos[i2]);
} else {
int tlast, xlast;
if (i1 < 0 || (i2 < a && left.back() < right.back())) {
tlast = right.back();
xlast = pos[i2];
} else {
tlast = left.back();
xlast = pos[i1];
}
printf("The last ant will fall down in %d seconds - started at %d.\n", tlast, xlast);
}
}
}