Advertisement
daniliambo

Untitled

Oct 1st, 2023
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. #include <algorithm>
  2. #include <string>
  3. #include <vector>
  4. #include <iostream>
  5. #include <sstream>
  6. #include <cmath>
  7. #include <limits>
  8. using std::cin;
  9. using std::cout;
  10. using std::max;
  11. using std::string;
  12. using std::vector;
  13.  
  14. vector<int> convert(std::string &line, char is_A)
  15. {
  16. std::stringstream iss(line);
  17. std::vector<int> myNumbers;
  18. int number;
  19.  
  20. if (is_A)
  21. {
  22. myNumbers.push_back(-1);
  23. }
  24. else
  25. {
  26. myNumbers.push_back(pow(10, 5));
  27. }
  28.  
  29. while (iss >> number)
  30. {
  31. myNumbers.push_back(number);
  32. }
  33.  
  34. if (is_A)
  35. {
  36. myNumbers.push_back(pow(10, 5));
  37. }
  38. else
  39. {
  40. myNumbers.push_back(-1);
  41. }
  42.  
  43. return myNumbers;
  44. }
  45.  
  46. vector<int> convert(std::string &line)
  47. {
  48. std::stringstream iss(line);
  49. std::vector<int> myNumbers;
  50. int number;
  51.  
  52. while (iss >> number)
  53. {
  54. myNumbers.push_back(number);
  55. }
  56. return myNumbers;
  57. }
  58.  
  59. vector<vector<int>> input(int rows, bool is_A)
  60. {
  61. vector<vector<int>> table;
  62. table.reserve(rows);
  63.  
  64. for (int row = 0; row < rows; ++row)
  65. {
  66. std::string line;
  67. getline(cin, line);
  68. table.push_back(convert(line, is_A));
  69. }
  70.  
  71. return table;
  72. }
  73.  
  74. int func(vector<int> const &A, vector<int> const &B, int mid)
  75. {
  76. return B[mid] - A[mid];
  77. }
  78.  
  79. int argmin(vector<int> const &vec)
  80. {
  81. int min = std::numeric_limits<int>::max();
  82. int idx = 0;
  83. for (int i = 0; i < static_cast<int>(vec.size()); ++i)
  84. {
  85. if (vec[i] < min)
  86. {
  87. min = vec[i];
  88. idx = i;
  89. }
  90. }
  91. return idx;
  92. }
  93.  
  94. int binary_search(vector<int> const &A_table, vector<int> const &B_table, int length)
  95. {
  96. int ll = -1;
  97. int rr = static_cast<int>(A_table.size()) + 1;
  98.  
  99. while (rr - ll > 0)
  100. {
  101. int mid = floor((ll + rr) / 2);
  102.  
  103. if (func(A_table, B_table, mid) > 0)
  104. {
  105. ll = mid + 1;
  106. }
  107. else
  108. {
  109. rr = mid;
  110. }
  111. }
  112.  
  113. // костыль для граничных случаев
  114. vector<int> res = {max(A_table[rr - 1], B_table[rr - 1]),
  115. max(A_table[rr], B_table[rr]),
  116. max(A_table[rr + 1], B_table[rr + 1])};
  117.  
  118. int add = argmin(res);
  119. return ll + (add - 1);
  120. }
  121.  
  122. int solution(vector<int> const &A_index,
  123. vector<int> const &B_index, int length)
  124. {
  125. int ans;
  126. ans = binary_search(A_index, B_index, length);
  127.  
  128. return ans;
  129. }
  130.  
  131. void process(vector<vector<int>> const &A_table,
  132. vector<vector<int>> const &B_table, int length)
  133. {
  134. string amount_of_requests;
  135. getline(cin, amount_of_requests);
  136. int amount_of_requests_int = stoi(amount_of_requests);
  137.  
  138. int index = 0;
  139. int a_table_index;
  140. int b_table_index;
  141.  
  142. while (index < amount_of_requests_int)
  143. {
  144. string request;
  145. getline(cin, request);
  146. vector<int> indices = convert(request);
  147. a_table_index = indices[0] - 1;
  148. b_table_index = indices[1] - 1;
  149.  
  150. cout << solution(A_table[a_table_index],
  151. B_table[b_table_index], length)
  152. << '\n';
  153. ++index;
  154. }
  155. }
  156.  
  157. int main()
  158. {
  159. std::ios_base::sync_with_stdio(false);
  160. std::cin.tie(nullptr);
  161.  
  162. string params_str;
  163. getline(cin, params_str);
  164.  
  165. vector<int> params_vec = convert(params_str);
  166.  
  167. int n_a_table = params_vec[0];
  168. int m_b_table = params_vec[1];
  169. int length = params_vec[2] + 2;
  170.  
  171. vector<vector<int>> A_table = input(n_a_table, true);
  172. vector<vector<int>> B_table = input(m_b_table, false);
  173.  
  174. process(A_table, B_table, length);
  175. return 0;
  176. }
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement