Go to diff to previous submission
#include <limits.h> #include <cstdlib> #include <cstdio> #include <algorithm> using namespace std; int getLen(int length, int pos, char dir) { int max; if (dir=='L') { max=pos; } else { max = length-pos; } return max; } char ants[100001]; int main(){ int length; int antsCount; int prevCount = 0; for (int i=0; i<100001; i++) { ants[i] = '0'; } while (scanf("%d %d ", &length, &antsCount) == 2) { for (int i=0; i<100001; i++) { ants[i] = '0'; } prevCount = antsCount; int maxCount=0; int maxPos[4]; int max = 0; for (int curAnts=0; curAnts < antsCount; curAnts++) { int pos; char dir; scanf("%d %c ", &pos, &dir); ants[pos] = dir; int curLen = getLen(length, pos, dir); if (curLen > max) { maxPos[0] = pos; maxCount = 1; max = curLen; } else if (curLen == max) { maxPos[maxCount++] = pos; } } for (int i=0; i<maxCount; i++) { char myDir = ants[maxPos[i]]; int myPos = maxPos[i]; for (int j=maxPos[i]; j<100001 && j>=0;) { if (myDir == 'L') { if (myPos != j) { if (ants[j] == 'R') { maxPos[i] = j; } else if (ants[j] == 'L') { break; } } j--; } else { if (myPos != j) { if (ants[j] == 'L') { maxPos[i] = j; } else if (ants[j] == 'R') { break; } } j++; } } } printf("The last ant will fall down in %d seconds - started at ", max); if (maxCount == 1) { printf("%d.\n", maxPos[0]); } else { if (maxPos[0] > maxPos[1]) { printf("%d and %d.\n", maxPos[1], maxPos[0]); } else { printf("%d and %d.\n", maxPos[0], maxPos[1]); } } } }