Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6. int n;
  7. cin >> n;
  8. int a[n];
  9. for (int i = 0; i < n; ++i) {
  10. cin >> a[i];
  11. }
  12. int m;
  13. cin >> m;
  14. int b[m];
  15. for (int i = 0; i < m; ++i) {
  16. cin >> b[i];
  17. }
  18. int j = 0;
  19. int ans = 1000000000;
  20. int ans1, ans2;
  21. for (int i = 0; i < n; ++i) {
  22. int r = abs(b[j] - a[i]);
  23. while (j < m && abs(b[j] - a[i]) <= r) {
  24. r = abs(b[j] - a[i]);
  25. ++j;
  26. }
  27. --j;
  28. if (abs(a[i] - b[j]) < ans) {
  29. ans = abs(a[i] - b[j]);
  30. ans1 = a[i];
  31. ans2 = b[j];
  32. }
  33. }
  34. cout << ans1 << " " << ans2;
  35. return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement