Source code for submission s1397

Go to diff to previous submission

ants.cpp

  1. #include <cstdio>
  2. #include <vector>
  3. #include <stack>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. inline bool is_inside(pair<int, int> place, int ants){
  8. return place.first >= 0 && place.first < ants && place.second < ants && place.second >= 0;
  9. }
  10.  
  11. inline bool have_conflict(pair<int, char> first, pair<int, char> second){
  12. return first.second == 'R' && second.second == 'L';
  13. }
  14.  
  15. inline void flip(pair<int, int> positions, vector<pair<int, char> >& vect){
  16. vect[positions.first].second = 'L';
  17. vect[positions.second].second = 'R';
  18. }
  19.  
  20. inline int maximum(int first, int second){
  21. return (first < second) ? second : first;
  22. }
  23.  
  24.  
  25. int main() {
  26. int len, ants, place, distance, max, number;
  27. char face;
  28. while(scanf("%d %d", &len, &ants) == 2) {
  29. vector<pair<int, char> > vect;
  30. vector<int> position(2);
  31. distance = -1;
  32. number = 0;
  33. max = -1;
  34. for (int i = 0; i < ants; i++) {
  35. scanf("%d %c", &place, &face);
  36. vect.push_back(make_pair(place, face));
  37. distance = (face == 'L') ? place : (len-place);
  38. if (distance > max) {
  39. number = 1;
  40. max = distance;
  41. } else if (distance == max) {
  42. number = 2;
  43. }
  44. }
  45.  
  46. sort(vect.begin(), vect.end());
  47.  
  48. pair<int, int> current;
  49. stack<pair<int, int> > stack;
  50. stack.push(make_pair(0,1));
  51. while (!stack.empty()) {
  52. current = stack.top();
  53. stack.pop();
  54. if (is_inside(current, ants)){
  55. if (have_conflict(vect[current.first], vect[current.second])){
  56. flip(current, vect);
  57. stack.push(make_pair(current.second, current.second+1));
  58. stack.push(make_pair(current.first - 1, current.first));
  59. } else {
  60. stack.push(make_pair(current.second, current.second + 1));
  61. }
  62. }
  63. }
  64.  
  65. int ii = 0;
  66. while (ii < ants && vect[ii].second == 'L'){
  67. ii++;
  68. }
  69. if (ii == 0){
  70. position[0] = vect[0].first;
  71. } else if (ii == ants) {
  72. position[0] = vect[ants-1].first;
  73. } else {
  74. if (number == 2){
  75. position[0] = vect[ii-1].first;
  76. position[1] = vect[ii].first;
  77. } else {
  78. position[0] = maximum(vect[ii].first, vect[ii-1].first);
  79. }
  80. }
  81.  
  82.  
  83. if (number == 1){
  84. printf("The last ant will fall down in %d seconds - started at %d.\n", max, position[0]);
  85. } else {
  86. printf("The last ant will fall down in %d seconds - started at %d and %d.\n", max, position[0], position[1]);
  87. }
  88.  
  89. }
  90. return 0;
  91. }
  92.  

Diff to submission s1298

ants.cpp

--- c4.s1298.cteam032.ants.cpp.0.ants.cpp
+++ c4.s1397.cteam032.ants.cpp.0.ants.cpp
@@ -14,9 +14,6 @@
 
 inline void flip(pair<int, int> positions, vector<pair<int, char> >& vect){
-        int x, y;
-        x = positions.first;
-        y = positions.second;
-        vect[x].second = 'L';
-        vect[y].second = 'R';
+        vect[positions.first].second = 'L';
+        vect[positions.second].second = 'R';
 }