Josif_tepe

Untitled

Nov 12th, 2025
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6. const int INF = 2e9;
  7. int main() {
  8.     ios_base::sync_with_stdio(false);
  9.     int n;
  10.     cin >> n;
  11.    
  12.     vector<int> podaroci(n);
  13.     for(int i = 0; i < n; i++) {
  14.         cin >> podaroci[i];
  15.     }
  16.    
  17.     int m;
  18.     cin >> m;
  19.    
  20.     vector<int> kutii(m);
  21.     for(int i = 0; i < m; i++) {
  22.         cin >> kutii[i];
  23.     }
  24.    
  25.     sort(podaroci.begin(), podaroci.end());
  26.     sort(kutii.begin(), kutii.end());
  27.    
  28.     int najmala_razlika = INF;
  29.     int podarok = -1;
  30.     for(int i = 0; i < n; i++) {
  31.         int idx = lower_bound(kutii.begin(), kutii.end(), podaroci[i]) - kutii.begin();
  32.        
  33.         if(idx >= 0 and idx < m) {
  34.             int razlika = kutii[idx] - podaroci[i];
  35.            
  36.             if(najmala_razlika >= razlika) {
  37.                 najmala_razlika = razlika;
  38.                
  39.                
  40.                 if(podaroci[i] > podarok) {
  41.                     podarok = podaroci[i];
  42.                 }
  43.             }
  44.         }
  45.        
  46.     }
  47.    
  48.     cout << podarok << endl;
  49.    
  50.    
  51.    
  52.     return 0;
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment