Source code for submission s1216

ants.cpp

  1.  
  2. #include <stdio.h>
  3. #include <list>
  4. #include <iostream>
  5.  
  6. class Ant
  7. {
  8. public:
  9. int facing;
  10. int pos;
  11. Ant(int fac, int p) : facing(fac), pos(p) {}
  12. };
  13.  
  14. #define FACE_R 0
  15. #define FACE_L 1
  16.  
  17. bool swapped;
  18.  
  19. void swap(int & a, int & b)
  20. {
  21. int temp = a;
  22. a = b;
  23. b = temp;
  24. swapped = true;
  25. }
  26.  
  27. using namespace std;
  28.  
  29. bool compare(const Ant & a, const Ant & b)
  30. {
  31. if (a.pos < b.pos)
  32. return 1;
  33. return 0;
  34. }
  35.  
  36. int main()
  37. {
  38. int ants_num, lumber_size;
  39. int ant1, ant2 = -1;
  40. int max_length = 0;
  41. int number_facings = 0;
  42. list<Ant> ants_array;
  43. while(scanf("%d%d", &lumber_size, &ants_num) == 2)
  44. {
  45. ants_array.clear();
  46. max_length = 0;
  47. number_facings = 0;
  48. ant2 = -1;
  49. for (int i = 0; i < ants_num; i++)
  50. {
  51. int pos;
  52. char facing;
  53. scanf("%d %c", &pos, &facing);
  54. ants_array.insert(ants_array.end(), Ant(facing == 'R' ? FACE_R : FACE_L, pos));
  55. int distance = facing == 'R' ? lumber_size - pos : pos;
  56. if (distance > max_length)
  57. max_length = distance;
  58. if (facing == 'L')
  59. number_facings++;
  60. }
  61. ants_array.sort(compare);
  62.  
  63. list<Ant>::iterator itr = ants_array.begin();
  64. for (int i = 0; i < number_facings; i++)
  65. {
  66. itr++;
  67. }
  68. list<Ant>::iterator first = ants_array.begin();
  69. list<Ant>::iterator last = --ants_array.end();
  70. int num2 = itr->pos;
  71. itr--;
  72. int num1 = itr->pos;
  73.  
  74. ant1 = 0;
  75.  
  76. while (first->facing == FACE_L && first != ants_array.end())
  77. first++;
  78. while (last->facing == FACE_R && last != ants_array.begin())
  79. last--;
  80. if (first != ants_array.end() && first->pos > lumber_size - last->pos)
  81. {
  82. ant1 = num1;
  83. }
  84.  
  85. if (first != last && first->pos < lumber_size - last->pos)
  86. {
  87. ant1 = num2;
  88. }
  89.  
  90. if (first != last && first != ants_array.end() && first->pos == lumber_size - last->pos)
  91. {
  92. ant1 = num1;
  93. ant2 = num2;
  94. }
  95. /*
  96. if (itr == ants_array.end())
  97. {
  98. ant1 = (--itr)->pos;
  99. }
  100. else
  101. if (itr == ants_array.begin())
  102. {
  103. ant1 = itr->pos;
  104. }
  105. else
  106. {
  107. int pos1 = lumber_size - itr->pos;
  108. int num1 = itr->pos;
  109. int pos2 = (--itr)->pos;
  110. int num2 = itr->pos;
  111. cout << num1 << " "<< num2 << endl;
  112. if (pos1 > pos2)
  113. ant1 = num1;
  114. else if (pos1 < pos2)
  115. ant1 = num2;
  116. else
  117. {
  118. ant1 = num1;
  119. ant2 = num2;
  120. }
  121. }*/
  122. printf("The last ant will fall down in %d seconds - started at %d",max_length, ant1);
  123. if ( ant2 != -1)
  124. printf(" and %d",ant2);
  125.  
  126. printf(".\n");
  127. }
  128.  
  129. }
  130.