Go to diff to previous submission
#include <iostream> #include <vector> #include <algorithm> using namespace std; class ant_info { public: int pos; char dir; ant_info(int pos, char dir) : pos(pos), dir(dir) {} static bool cmp(const ant_info& fst, const ant_info& snd) { return fst.pos < snd.pos; } }; int main() { vector<ant_info> ants; int maxl; int maxr; int startl; int startr; int length; int n; int pos; int dist; char dir; while (cin >> length >> n) { ants = vector<ant_info>(); maxl = maxr = -1; startl = startr = -1; size_t i; for (i = 0; i < n; i++) { cin >> pos >> dir; ants.push_back(ant_info(pos, dir)); if (dir == 'L') { if (pos > maxl) { maxl = pos; } } else { dist = length - pos; if (dist > maxr) { maxr = dist; } } } sort(ants.begin(), ants.end(), ant_info::cmp); int mid = 0; for (i = 0; i < ants.size(); i++) { bool left = ants[i].dir == 'L'; if (left) mid++; } if (mid > 0) startl = ants[mid - 1].pos; if (mid < static_cast<int>(ants.size())) startr = ants[mid].pos; cout << "The last ant will fall down in " << (maxl >= maxr ? maxl : maxr) << " seconds - started at "; if (maxl == maxr) { if (startl > startr) swap(startl, startr); cout << startl << " and " << startr; } else { cout << (maxl > maxr ? startl : startr); } cout << "." << endl; } return 0; }