Advertisement
SergeyPGUTI

9.9(Таинство суммы)

Mar 27th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>     /* srand, rand */
  3. #include <time.h>       /* time */
  4. #include <vector>
  5. #include <algorithm>    // std::sort
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int main()
  11. {
  12.     int n,m,x,low,high,mid;
  13.     cin>>n;
  14.     int *arr=new int[n];
  15.     for (int i=0;i<n;i++)
  16.     {
  17.         cin>>arr[i];
  18.     }
  19.     cin>>m;
  20.     int *arr2=new int[m];
  21.     for (int i=0;i<m;i++)
  22.     {
  23.         cin>>arr2[i];
  24.     }
  25.     for(int i=0;i<m;i++)
  26.     {
  27.         x=arr2[i];
  28.         low=0;
  29.         high=n;
  30.         while(high-low>1)
  31.         {
  32.             mid=low+(high-low)/2;
  33.             if (x+arr[mid]>10000)
  34.                 high=mid;
  35.             else if(x+arr[mid]<10000)
  36.                 low=mid;
  37.             else {
  38.                 cout<<"YES";
  39.                 return 0;
  40.             }
  41.         }
  42.         if (arr[low]+x==10000)
  43.         {
  44.             cout<<"YES";
  45.             return 0;
  46.         }
  47.         if (arr[high]+x==10000)
  48.         {
  49.             cout<<"YES";
  50.             return 0;
  51.         }
  52.  
  53.     }
  54.     cout<<"NO";
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement