ants.cpp
#include <cstdio>
#include <cstdlib>
#define MAX 100010
int l,n;
char dirs[MAX];
int ants[MAX];
int start,end;
int lcnt;
int maxL,maxR;
int pos2,pos1;
int getLastL(){
int ctr=0;
for(int i=0;i<=l;i++){
if(ants[i]!=-1)
ctr++;
if(ctr == lcnt)
return i;
}
return n+100;
}
int getLastR(){
int ctr=0;
for(int i=0;i<=l;i++){
if(ants[i]!=-1)
ctr++;
if(ctr == lcnt+1)
return i;
}
return n+100;
}
int main(int argc, char ** argv){
int pos;
char dir;
bool noL,noR;
int pom;
while(scanf("%d %d",&l,&n) == 2){
noL = noR = true;
lcnt=0;
maxR = maxL = 0;
pos = 0;
for(int i=0; i <= l;i++){
ants[i] = -1;
}
for(int i=0; i < n;i++){
scanf("%d %c",&pos,&dir);
if(dir == 'R'){
noR = false;
ants[pos] = l - pos;
if(ants[maxL] < ants[pos]){
maxL = pos;
}
}
else{
lcnt++;
noL = false;
ants[pos] = pos;
if(ants[maxR] < ants[pos]){
maxR = pos;
}
}
}
// no left oriented
if(noL){
printf("The last ant will fall down in %d seconds - started at %d.\n",ants[maxL],maxL);
continue;
}
// no right oriented
if(noR){
printf("The last ant will fall down in %d seconds - started at %d.\n",ants[maxR],maxR);
continue;
}
//printf("%d %d \n",maxL,maxR);
// for(int i=0;i<=l;i++){
//if(ants[i]!=-1)
// printf("%d ",i);
//printf("\n");
// }
if(ants[maxL] > ants[maxR]){
pos1 = getLastL();
printf("The last ant will fall down in %d seconds - started at %d.\n",ants[maxL],pos1);
continue;
}
if(ants[maxR] > ants[maxL]){
pos1 = getLastR();
printf("The last ant will fall down in %d seconds - started at %d.\n",ants[maxR],pos1);
continue;
}
pos1 = getLastR();
pos2 = getLastL();
if(pos2 < pos1){
pom = pos1;
pos1 = pos2;
pos2 = pom;
}
printf("The last ant will fall down in %d seconds - started at %d and %d.\n",ants[maxR],pos1,pos2);
}
return 0;
}