Advertisement
Guest User

ыыыы

a guest
Jun 24th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. #include <bitset>
  6. #include <iomanip>
  7. #include <math.h>
  8. #include <stdlib.h>
  9. #include <map>
  10. #include <unordered_map>
  11. #include <tuple>
  12. #include <cmath>
  13. #include <functional>
  14. #include <algorithm>
  15. #include <set>
  16. #include <unordered_set>
  17. #include <deque>
  18. #include <numeric>
  19. #include <cstring>
  20.  
  21.  
  22. struct v {
  23. int l;
  24. int r;
  25. std::string name;
  26. int type;
  27. };
  28.  
  29.  
  30. const std::vector<int> def_year = {30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  31. const std::vector<int> spc_year = {30, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  32. std::vector<v> events;
  33.  
  34.  
  35. bool cmp(const v& v1, const v& v2) {
  36. if (v1.l < v2.l)
  37. return true;
  38. else if (v1.l == v2.l && v1.r < v2.r)
  39. return true;
  40. else if (v1.l == v2.l && v1.r == v2.r && v1.type > v2.type)
  41. return true;
  42. else
  43. return false;
  44. }
  45.  
  46.  
  47. bool operator<(const v& v1, const v& v2) {
  48. return cmp(v1, v2);
  49. }
  50.  
  51.  
  52. int date_to_num(std::string s) {
  53. int days = (s[0] - 48) * 10 + (s[1] - 48);
  54. int months = (s[3] - 48) * 10 + (s[4] - 48);
  55. int years = (s[6] - 48) * 1000 + (s[7] - 48) * 100 + (s[8] - 48) * 10 + (s[9] - 48);
  56. int number = 0;
  57. int cur_year = 1500;
  58. while (cur_year < years) {
  59. if ((cur_year % 4 == 0 && cur_year % 100 != 0) || cur_year % 400 == 0)
  60. number += 366;
  61. else
  62. number += 365;
  63. cur_year++;
  64. }
  65. if ((cur_year % 4 == 0 && cur_year % 100 != 0) || cur_year % 400 == 0) {
  66. for (int i = 0; i < months - 1; i++)
  67. number += spc_year[i];
  68. } else {
  69. for (int i = 0; i < months - 1; i++)
  70. number += def_year[i];
  71. }
  72. number += days;
  73. return number;
  74. }
  75.  
  76.  
  77. void num_to_date(int number) {
  78. int year = 1500;
  79. int months = 1;
  80. while (true) {
  81. if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
  82. if (number > 366)
  83. number -= 366;
  84. else
  85. break;
  86. else
  87. if (number > 365)
  88. number -= 365;
  89. else
  90. break;
  91. year++;
  92. }
  93. if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
  94. for (int i = 0; i < 12; i++) {
  95. if (number > spc_year[i])
  96. months++;
  97. else
  98. break;
  99. }
  100. else
  101. for (int i = 0; i < 12; i++) {
  102. if (number > def_year[i]) {
  103. months++;
  104. number -= def_year[i];
  105. } else
  106. break;
  107. }
  108. int days = number + 1;
  109. if (days < 10)
  110. std::cout << '0' << days << '.';
  111. else
  112. std::cout << days << '.';
  113. if (months < 10)
  114. std::cout << '0' << months << '.';
  115. else
  116. std::cout << months << '.';
  117. std::cout << year;
  118. }
  119.  
  120.  
  121. bool check(int M, int n) {
  122. std::unordered_set<std::string> names;
  123. int i = M;
  124. int j = 0;
  125. std::set<v> queue;
  126. while (j < events.size()) {
  127. while (j < events.size() && events[j].l <= i) {
  128. if (events[j].type == 0)
  129. queue.insert(events[j]);
  130. else if (events[j].type == 1)
  131. if (names.find(events[j].name) == names.end())
  132. return false;
  133. j++;
  134. }
  135. if (queue.size() != 0) {
  136. names.insert((*queue.begin()).name);
  137. queue.erase(*queue.begin());
  138. }
  139. i++;
  140. }
  141. return true;
  142. }
  143.  
  144.  
  145. int main() {
  146. freopen("input.txt", "r", stdin);
  147. freopen("output.txt", "w", stdout);
  148. std::ios::sync_with_stdio(false);
  149. std::cin.tie(0);
  150. std::cout.tie(0);
  151. int n;
  152. std::cin >> n;
  153. std::string name, date;
  154. int t;
  155. int min_L = (int) 1e9, min_R = (int) 1e9;
  156. for (int i = 0; i < n; i++) {
  157. std::cin >> name >> date >> t;
  158. int num = date_to_num(date);
  159. events.push_back({num - t, num, name, 0});
  160. events.push_back({num, num - t, name, 1});
  161. min_L = std::min(min_L, num - t);
  162. min_R = std::min(min_R, num - 1);
  163. }
  164. std::sort(events.begin(), events.end(), cmp);
  165. int L = min_L, R = min_R;
  166. while (R - L > 1) {
  167. int M = (L + R) / 2;
  168. if (check(M, n))
  169. L = M;
  170. else
  171. R = M;
  172. }
  173. if (check(L, n)) {
  174. //std::cout << L << '\n';
  175. num_to_date(L);
  176. } else
  177. std::cout << "Impossible";
  178. return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement