Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <string>
- #include <vector>
- #include <iostream>
- #include <sstream>
- #include <cmath>
- #include <limits>
- using std::cin;
- using std::cout;
- using std::max;
- using std::string;
- using std::vector;
- vector<int> convert(std::string &line, char is_A)
- {
- std::stringstream iss(line);
- std::vector<int> myNumbers;
- int number;
- if (is_A)
- {
- myNumbers.push_back(-1);
- }
- else
- {
- myNumbers.push_back(pow(10, 5));
- }
- while (iss >> number)
- {
- myNumbers.push_back(number);
- }
- if (is_A)
- {
- myNumbers.push_back(pow(10, 5));
- }
- else
- {
- myNumbers.push_back(-1);
- }
- return myNumbers;
- }
- vector<int> convert(std::string &line)
- {
- std::stringstream iss(line);
- std::vector<int> myNumbers;
- int number;
- while (iss >> number)
- {
- myNumbers.push_back(number);
- }
- return myNumbers;
- }
- vector<vector<int>> input(int rows, bool is_A)
- {
- vector<vector<int>> table;
- table.reserve(rows);
- for (int row = 0; row < rows; ++row)
- {
- std::string line;
- getline(cin, line);
- table.push_back(convert(line, is_A));
- }
- return table;
- }
- int func(vector<int> const &A, vector<int> const &B, int mid)
- {
- return B[mid] - A[mid];
- }
- int argmin(vector<int> const &vec)
- {
- int min = std::numeric_limits<int>::max();
- int idx = 0;
- for (int i = 0; i < static_cast<int>(vec.size()); ++i)
- {
- if (vec[i] < min)
- {
- min = vec[i];
- idx = i;
- }
- }
- return idx;
- }
- int binary_search(vector<int> const &A_table, vector<int> const &B_table, int length)
- {
- int ll = -1;
- int rr = static_cast<int>(A_table.size()) + 1;
- while (rr - ll > 0)
- {
- int mid = floor((ll + rr) / 2);
- if (func(A_table, B_table, mid) > 0)
- {
- ll = mid + 1;
- }
- else
- {
- rr = mid;
- }
- }
- // костыль для граничных случаев
- vector<int> res = {max(A_table[rr - 1], B_table[rr - 1]),
- max(A_table[rr], B_table[rr]),
- max(A_table[rr + 1], B_table[rr + 1])};
- int add = argmin(res);
- return ll + (add - 1);
- }
- int solution(vector<int> const &A_index,
- vector<int> const &B_index, int length)
- {
- int ans;
- ans = binary_search(A_index, B_index, length);
- return ans;
- }
- void process(vector<vector<int>> const &A_table,
- vector<vector<int>> const &B_table, int length)
- {
- string amount_of_requests;
- getline(cin, amount_of_requests);
- int amount_of_requests_int = stoi(amount_of_requests);
- int index = 0;
- int a_table_index;
- int b_table_index;
- while (index < amount_of_requests_int)
- {
- string request;
- getline(cin, request);
- vector<int> indices = convert(request);
- a_table_index = indices[0] - 1;
- b_table_index = indices[1] - 1;
- cout << solution(A_table[a_table_index],
- B_table[b_table_index], length)
- << '\n';
- ++index;
- }
- }
- int main()
- {
- std::ios_base::sync_with_stdio(false);
- std::cin.tie(nullptr);
- string params_str;
- getline(cin, params_str);
- vector<int> params_vec = convert(params_str);
- int n_a_table = params_vec[0];
- int m_b_table = params_vec[1];
- int length = params_vec[2] + 2;
- vector<vector<int>> A_table = input(n_a_table, true);
- vector<vector<int>> B_table = input(m_b_table, false);
- process(A_table, B_table, length);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement