#include <stdio.h>
#define LEFT 'L'
#define RIGHT 'R'
#define NONE '\0'
int falltime(int position, char direction, int length) {
if (direction == 'R') {
return length - position;
}
return position;
}
char directions[100000];
int main(void) {
int length
, count
, position
, time, max_time
, i
, l
, r
, last_left_going_left
, last_left_going_right
, last_right_going_left
, last_right_going_right
, max
, max2
, pos
, pos2
; int posibilities[4], posibilities_directions[4];
char direction;
while (scanf("%d %d", &length
, &count
) == 2) { for (i = 0; i <= length; ++i) {
directions[i] = NONE;
}
max_time = -1;
i = count;
while (i--) {
scanf("%d %c", &position
, &direction
);
time = falltime
(position
, direction
, length
);
}
directions[position] = direction;
}
last_left_going_right = length + 1;
last_right_going_left = -1;
last_left_going_left = -1;
last_right_going_right = -1;
for (i = count, l = 0, r = length; l <= length && r >= 0; ++l, --r) {
if (directions[l] == RIGHT) {
last_left_going_right = l;
} else if (directions[l] == LEFT) {
if (last_left_going_right < l) {
directions[last_left_going_right] = NONE;
--i;
last_left_going_left = last_left_going_right;
directions[l] = RIGHT;
last_left_going_right = l;
} else {
directions[l] = NONE;
--i;
last_left_going_left = l;
}
}
if (directions[r] == LEFT) {
last_right_going_left = r;
} else if (directions[r] == RIGHT) {
if (last_right_going_left > r) {
directions[last_right_going_left] = NONE;
--i;
last_right_going_right = last_right_going_left;
directions[l] = LEFT;
last_right_going_left = r;
} else {
directions[r] = NONE;
--i;
last_right_going_right = r;
}
}
}
printf("The last ant will fall down in %d seconds - started at ", max_time
);
i = 0;
if (last_left_going_right != length + 1) {
posibilities[i] = last_left_going_right;
posibilities_directions[i] = RIGHT;
++i;
}
if (last_left_going_left >= 0) {
posibilities[i] = last_left_going_left;
posibilities_directions[i] = LEFT;
++i;
}
if (last_right_going_left >= 0) {
posibilities[i] = last_right_going_left;
posibilities_directions[i] = LEFT;
++i;
}
if (last_right_going_right >= 0) {
posibilities[i] = last_right_going_right;
posibilities_directions[i] = RIGHT;
++i;
}
max = max2 = -1;
pos = pos2 = -1;
i -= 1;
while (i >= 0) {
//printf("[%d] = %d %c\n", i, posibilities[i], posibilities_directions[i]);
time = falltime
(posibilities
[i
], posibilities_directions
[i
], length
); //printf("max is %d\n", posibilities[i]);
max2 = max;
pos2 = pos;
pos = posibilities[i];
}
--i;
}
if (max == max2 && pos != pos2) {
if (pos < pos2) {
printf("%d and %d.\n", pos
, pos2
); } else {
printf("%d and %d.\n", pos2
, pos
); }
} else {
}
}
return 0;
}