ants.c
#include <stdio.h>
#include <string.h>
#define false 0
#define true 1
char inv(char c){
return c=='L' ? 'R' : 'L';
}
int main( int agrc, char * argv[] ) {
char array[100002],c, remL, remR;
int i, wood=10, ants, coo, remaining, pL, pR, timeL, timeR;
int waitL, waitR;
int distL, distR;
int max=-1, len;
while( scanf("%d %d", &wood
, &ants
) != EOF
) { waitL = false;
waitR = false;
pL = wood;
pR = 0;
max = -1;
for( i = 0 ; i < ants ; i++ ) {
scanf("%d %c", &coo
, &c
); array[coo] = c;
if(coo > pR) pR = coo;
if(coo < pL) pL = coo;
}
remaining = ants;
remL = 'L';
remR = 'R';
timeL = 0;
timeR = wood;
len = 0;
while(timeL < wood) {
if(array[timeL] == 'R'){
max = wood-len;
break;
}
timeL++;
len++;
}
len = 0;
while(timeR >= 0) {
if(array[timeR] == 'L'){
if(wood-len>max) max = wood-len;
break;
}
timeR--;
len++;
}
while(remaining > 2 && pL < pR) {
if(waitL == false){
while(array[pL] == ' ') pL++;
if(array[pL] == remL){
remaining--;
array[pL] = ' ';
pL++;
} else if(array[pL] == inv(remL)) {
remL = inv(remL);
if(waitR == false)
waitL = true;
waitR = false;
}
}
if(waitR == false && remaining > 2){
while(array[pR] == ' ') pR--;
if(array[pR] == remR){
remaining--;
array[pR] = ' ';
pR--;
} else if(array[pR] == inv(remR)){
remR = inv(remR);
if(waitL == false)
waitR = true;
waitL = false;
}
}
//for( i = 0 ; i < wood ; i++ )
// printf("%c", array[i]);
//printf(", rem: %c %c, p: %2d %2d, wait: %d %d, remaining: %d\n", remL, remR, pL, pR, waitL, waitR, remaining);
}
while(array[pL] == ' ') pL++;
while(array[pR] == ' ') pR--;
distL = array[pL] == ' ' ? wood+123 : pL;
distR = array[pL] == ' ' ? wood+123 : wood-pR;
printf("The last ant will fall down in %d seconds - ", max
); if(distL > distR || pL == pR){
printf("started at %d.\n", pL
); }
if(distR > distL && pL != pR){
printf("started at %d.\n", pR
); }
if(distR == distL){
printf("started at %d and %d.\n", pL
, pR
); }
}
return 0;
}