Source code for submission s1257

sablona.cc

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <set>
  6. #include <map>
  7. #include <string>
  8. #include <cmath>
  9. #include <cstdlib>
  10. #include <cstring>
  11. #include <sstream>
  12.  
  13. #include <stdio.h>
  14. #include <ctype.h>
  15. #include <math.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18.  
  19. using namespace std;
  20.  
  21. #define X first
  22. #define Y second
  23. #define MP make_pair
  24. #define PB push_back
  25. #define SZ size
  26.  
  27. struct Dvojice {
  28. int zac;
  29. char smer;
  30. };
  31.  
  32. int compare (const void *a, const void *b) {
  33. struct Dvojice a1 = *(struct Dvojice*)a;
  34. struct Dvojice b1 = *(struct Dvojice*)b;
  35. return (a1.zac-b1.zac);
  36. }
  37.  
  38. int main(void)
  39. {
  40. int a, b, c, d, f, g, aa, i, ii, akt, akt2, j, k, ok2, ok, pom1, pom2, cas1, cas2, max;
  41. char e;
  42. struct Dvojice mrv[100005];
  43.  
  44. while (scanf("%d %d", &a, &b) > 0) {
  45. max = 0;
  46. for (i = 0; i < b; i++) {
  47. scanf("%d %c\n", &c, &e);
  48. mrv[i].zac = c;
  49. mrv[i].smer = e;
  50. if (e == 'R') {
  51. if (a - c > max)
  52. max = a - c;
  53. } else {
  54. if (c > max)
  55. max = c;
  56. }
  57. }
  58. qsort(mrv, b, sizeof(struct Dvojice), compare);
  59. d = 0;
  60. f = b - 1;
  61. ok2 = 1;
  62. while(d < f && ok2 == 1) {
  63. ok = 0;
  64. pom1 = d + 1;
  65. pom2 = f - 1;
  66. while (ok == 0) {
  67. if (mrv[pom1].smer != mrv[d].smer) {
  68. ok = 1;
  69. } else {
  70. pom1++;
  71. }
  72. if (pom1 > f) {
  73. ok = 1;
  74. pom1 = -1;
  75. }
  76. }
  77. ok = 0;
  78. while (ok == 0) {
  79. if (mrv[pom2].smer != mrv[f].smer) {
  80. ok = 1;
  81. } else {
  82. pom2++;
  83. }
  84. if (pom2 < d) {
  85. ok = 1;
  86. pom2 = -1;
  87. }
  88. }
  89. if (pom1 != -1) {
  90. cas1 = mrv[pom1].zac - mrv[d].zac;
  91. }
  92. if (pom2 != -1) {
  93. cas2 = mrv[f].zac - mrv[pom2].zac;
  94. }
  95. if (pom1 != -1) {
  96. if (cas1 == cas2) {
  97. if (mrv[pom2].smer == 'R')
  98. mrv[pom2].smer = 'L';
  99. else
  100. mrv[pom2].smer = 'R';
  101. f--;
  102. if (mrv[pom1].smer == 'R')
  103. mrv[pom1].smer = 'L';
  104. else
  105. mrv[pom1].smer = 'R';
  106. d++;
  107. } else if (cas1 > cas2) {
  108. if (mrv[pom2].smer == 'R')
  109. mrv[pom2].smer = 'L';
  110. else
  111. mrv[pom2].smer = 'R';
  112. f--;
  113. } else {
  114. if (mrv[pom1].smer == 'R')
  115. mrv[pom1].smer = 'L';
  116. else
  117. mrv[pom1].smer = 'R';
  118. d++;
  119. }
  120. } else {
  121. ok2 = 0;
  122. }
  123. //printf("%d %d\n", d, f);
  124. }
  125. if (ok2 == 0) {
  126. if (mrv[d].smer == 'L')
  127. printf("The last ant will fall down in %d seconds - started at %d.\n", max, mrv[f].zac);
  128. else
  129. printf("The last ant will fall down in %d seconds - started at %d.\n", max, mrv[d].zac);
  130. } else {
  131. if (a - mrv[d].zac > mrv[f].zac)
  132. printf("The last ant will fall down in %d seconds - started at %d.\n", max, mrv[d].zac);
  133. else if (a - mrv[d].zac > mrv[f].zac)
  134. printf("The last ant will fall down in %d seconds - started at %d.\n", max, mrv[f].zac);
  135. else
  136. printf("The last ant will fall down in %d seconds - started at %d and %d.\n", max, mrv[f].zac, mrv[d].zac);
  137. }
  138. }
  139.  
  140. return 0;
  141. }
  142.