fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. pair<int, int> solve(vector<int>& A, vector<int>& B) {
  5. pair<int, int> result;
  6. int minimumAbsoluteValue = INT_MAX;
  7.  
  8. int n = B.size();
  9. vector<pair<int,int>> sortedB;
  10. for(int i = 0; i < n; i++) {
  11. sortedB.push_back({B[i], i});
  12. }
  13.  
  14. sort(sortedB.begin(), sortedB.end());
  15.  
  16. for(int i = 0; i < A.size(); i++) {
  17. int lower = lower_bound(sortedB.begin(), sortedB.end(), make_pair(-A[i], INT_MIN)) - sortedB.begin();
  18. // break;
  19. if(lower != n) {
  20. int absoluteValue = abs(A[i] + sortedB[lower].first);
  21. if(absoluteValue < minimumAbsoluteValue) {
  22. minimumAbsoluteValue = absoluteValue;
  23. result.first = i + 1;
  24. result.second = sortedB[lower].second + 1;
  25. }
  26. }
  27. if(lower != 0) {
  28. lower--;
  29. int absoluteValue = abs(A[i] + sortedB[lower].first);
  30. if(absoluteValue < minimumAbsoluteValue) {
  31. minimumAbsoluteValue = absoluteValue;
  32. result.first = i + 1;
  33. result.second = sortedB[lower].second + 1;
  34. }
  35. }
  36.  
  37. // cout << minimumAbsoluteValue << endl;
  38. }
  39.  
  40. return result;
  41. }
  42.  
  43. int main() {
  44. int m, n; cin >> m >> n;
  45. vector<int> A(m);
  46. vector<int> B(n);
  47.  
  48. for(int& x : A) cin >> x;
  49. for(int& x : B) cin >> x;
  50.  
  51. pair<int, int> res = solve(A, B);
  52. cout << res.first << " " << res.second;
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0.01s 5276KB
stdin
4 5 
1 8 2 9 
-5 -6 3 -7 -4
stdout
2 4