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; } } int prevPos=-1; 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 (ants[j] == 'R' || ants[j] == 'L') { if (myPos != j) { maxPos[i] = j; prevPos = j; if (ants[j] == 'L') { break; } } else { prevPos = j; } } j--; } else { if (ants[j] == 'R' || ants[j] == 'L') { if (myPos != j) { maxPos[i] = j; prevPos = j; if (ants[j] == 'R') { break; } } else { prevPos = j; } } 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]); } } } }
--- c4.s1254.cteam027.ants.cpp.0.ants.cpp +++ c4.s1278.cteam027.ants.cpp.0.ants.cpp @@ -56,4 +56,5 @@ } + int prevPos=-1; for (int i=0; i<maxCount; i++) { char myDir = ants[maxPos[i]]; @@ -62,20 +63,26 @@ 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') { + if (ants[j] == 'R' || ants[j] == 'L') { + if (myPos != j) { maxPos[i] = j; - break; + prevPos = j; + if (ants[j] == 'L') { + break; + } + } else { + prevPos = j; } } j--; } else { - if (myPos != j) { - if (ants[j] == 'L') { - maxPos[i] = j; - } else if (ants[j] == 'R') { + if (ants[j] == 'R' || ants[j] == 'L') { + if (myPos != j) { maxPos[i] = j; - break; + prevPos = j; + if (ants[j] == 'R') { + break; + } + } else { + prevPos = j; } }