ants.cpp
#include <cstdio>
using namespace std;
int main(void) {
int count, length;
while(scanf("%d%d\n", &length, &count) == 2) {
int leftmost = -1, rightmost = -1;
bool ants[100000];
for(int i = 0; i < 100000; ++i) ants[i] = false;
int pos;
char dir;
int toLeft = 0;
for(int a = 0; a < count; ++a) {
scanf("%d %c\n", &pos, &dir);
ants[pos] = true;
if(dir == 'R' && (leftmost == -1 || pos < leftmost)) {
leftmost = pos;
}
else if(dir == 'L' && (rightmost == -1 || pos > rightmost)) {
rightmost = pos;
}
if(dir == 'L') ++toLeft;
}
int maxTime = 0;
if(leftmost != -1) maxTime = length - leftmost;
if(rightmost != -1 && rightmost > maxTime) maxTime = rightmost;
printf("The last ant will fall down in %d seconds - started at ", maxTime);
if(rightmost == length - leftmost) {
int passed = 0;
for(int a = 0; a < length; ++a) {
if(!ants[a]) continue;
++passed;
if(passed == toLeft) {
printf("%d and ", a);
}
else if(passed == toLeft + 1) {
printf("%d", a);
break;
}
}
}
else if(maxTime == length - leftmost) {
int passed = 0;
for(int a = 0; a < length; ++a) {
if(!ants[a]) continue;
if(++passed == toLeft + 1) {
printf("%d", a);
break;
}
}
}
else {
int passed = 0;
for(int a = 0; a < length; ++a) {
if(!ants[a]) continue;
if(++passed == toLeft) {
printf("%d", a);
break;
}
}
}
printf(".\n");
}
return 0;
}