Advertisement
TAHMID37

Tahmid and numbers(D)

Jun 26th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. /*  TAHMID RAHMAN
  2.     DAMIAN FOREVER
  3.      MATH LOVER
  4.     NEVER GIVE UP
  5. */
  6. #include<bits/stdc++.h>
  7. using namespace std;
  8. #define pi acos(-1.0)
  9. #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  10. #define ll long long
  11. #define pb push_back
  12. #define fi first
  13. #define se second
  14. #define in insert
  15. #define mp make_pair
  16. #define GCD(a,b) __gcd(a,b);
  17. #define endl "\n"
  18. #define FRU freopen("out.txt","w",stdout)
  19. #define FRO freopen("in.txt","r",stdin)
  20. #define INFLL 9223372036854775807
  21. #define debug 0
  22. #define MAXN   100001
  23. //Don't hesitate to ask me if you don't understand my code.......Happy coding,Tahmid...;
  24. int main()
  25. {
  26.     fastio;
  27.     ll n,p,i,c=INFLL,x=-1,y=0,z=0;
  28.     cin>>n>>p;
  29.     vector<ll>v(p);
  30.     set<ll>s;
  31.     if(p==0)
  32.     {
  33.         cout<<n<<endl;
  34.         return 0;
  35.     }
  36.     for(i=0;i<p;i++)
  37.     {
  38.         cin>>v[i];
  39.         if(abs(n-v[i])<c)
  40.         {
  41.             c=abs(n-v[i]);
  42.             x=v[i];
  43.         }
  44.         s.in(v[i]);
  45.     }
  46.     for(i=x+1;;i++)
  47.     {
  48.         if(s.count(i)==0)
  49.         {
  50.             y=i;
  51.             break;
  52.         }
  53.     }
  54.     for(i=x-1;;i--)
  55.     {
  56.         if(s.count(i)==0)
  57.         {
  58.             z=i;
  59.             break;
  60.         }
  61.     }
  62.     if((y-x)<abs(z-x))
  63.     {
  64.         cout<<y<<endl;
  65.     }
  66.     else if(abs(z-x)<(y-x))
  67.         cout<<z<<endl;
  68.     else
  69.         cout<<min(y,z)<<endl;
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement