Advertisement
chillurbrain

9. Таинство суммы

May 21st, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. const int MAXN = 50000, MAX = 10000;
  7. int a[MAXN], b[MAXN];
  8. int n, k, m;
  9.  
  10. int main(int argc, char** argv) {
  11.     ios_base::sync_with_stdio(0);
  12.     cin >> n;
  13.     for(int i = 0; i < n; ++i) {
  14.         cin >> a[i];
  15.     }
  16.     cin >> k;
  17.     for(int i = 0; i < k; ++i) {
  18.         cin >> b[i];
  19.     }
  20.     sort(a, a+n);
  21.     sort(b, b+k);
  22.     for(int i = 0; i < n; ++i) {
  23.         const int cur = a[i];
  24.         int l = 0;
  25.         int r = k;
  26.         while(r-l > 1) { // binary search
  27.             m = (l+r)>>1;
  28.             if(cur+b[m] > MAX)
  29.                 r = m;
  30.             else
  31.                 l = m;
  32.         }
  33.         if(cur + b[l] == MAX) {
  34.             cout << "YES";
  35.             return 0;
  36.         }
  37.     }
  38.     cout << "NO";
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement