Last active
August 29, 2015 14:20
-
-
Save s25g5d4/c475864c12387a51b8a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <vector> | |
#include <array> | |
struct Ant | |
{ | |
int pos; | |
int order; | |
char direction; | |
}; | |
int main() | |
{ | |
using namespace std; | |
int test_case; | |
cin >> test_case; | |
for (int case_num = 1; case_num <= test_case; ++case_num) { | |
int pole_long; | |
int seconds; | |
int n; | |
cin >> pole_long >> seconds >> n; | |
vector<Ant> ants(n); | |
vector<int> original_order(n); | |
for (int order = 0; order < n; ++order) { | |
int pos; | |
char direction; | |
cin >> pos; | |
do { | |
cin.get(direction); | |
} while (direction != 'L' && direction != 'R'); | |
ants[order] = Ant {pos, order, direction}; | |
} | |
sort(ants.begin(), ants.end(), [](const Ant &a, const Ant &b) { | |
if (a.pos == b.pos) | |
return a.direction < b.direction; | |
return a.pos < b.pos; | |
}); | |
for (int i = 0; i < n; ++i) | |
original_order[ants[i].order] = i; | |
for_each(ants.begin(), ants.end(), [seconds](Ant &e) { | |
e.pos += seconds * (e.direction == 'L' ? -1 : 1); | |
}); | |
sort(ants.begin(), ants.end(), [](const Ant &a, const Ant &b) { | |
if (a.pos == b.pos) | |
return a.direction < b.direction; | |
return a.pos < b.pos; | |
}); | |
if (n > 1) { | |
for (auto it = ants.begin() + 1; it != ants.end(); ++it) { | |
if (it->pos == (it - 1)->pos) { | |
it->direction = 'T'; | |
(it - 1)->direction = 'T'; | |
} | |
} | |
} | |
cout << "Case #" << case_num << ':' << endl; | |
for (auto i : original_order) { | |
if (ants[i].pos < 0 || ants[i].pos > pole_long) { | |
cout << "Fell off"; | |
} | |
else { | |
cout << ants[i].pos << ' '; | |
if (ants[i].direction == 'T') | |
cout << "Turning"; | |
else | |
cout << ants[i].direction; | |
} | |
cout << endl; | |
} | |
cout << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment