Advertisement
lynulx

Untitled

Oct 27th, 2021
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6.  
  7. int min_element(int *nums){
  8.     if (nums[0] > nums[1]) return nums[0];
  9.     return nums[1];
  10. }
  11.  
  12.  
  13. int main(){
  14.     long long int start;
  15.     long long int olymp;
  16.     long long int portals_qnt;
  17.     cin>>start;
  18.     cin>>olymp;
  19.     cin>>portals_qnt;
  20.     long long int lp_from_start = -pow(10, 8);
  21.     long long int rp_from_start = pow(10, 8);
  22.     long long int lp_from_olymp = -pow(10, 8);
  23.     long long int rp_from_olymp = pow(10, 8);
  24.     for (int i = 0; i < portals_qnt; i++){
  25.         int portal_coord;
  26.         cin>>portal_coord;
  27.         if (portal_coord < olymp and portal_coord > lp_from_olymp) lp_from_olymp = portal_coord;
  28.         else if (portal_coord >= olymp and portal_coord < rp_from_olymp) rp_from_olymp = portal_coord;
  29.         if (portal_coord < start and portal_coord > lp_from_start) lp_from_start = portal_coord;
  30.         else if (portal_coord >= start and portal_coord < rp_from_start) rp_from_start = portal_coord;
  31.     }
  32.     long long int &time_to_olymp_from_portal = *min_element({abs(rp_from_olymp - olymp), abs(olymp - lp_from_olymp)});
  33.     long long int &time_to_start_from_portal = *min_element({abs(rp_from_start - start), abs(start - lp_from_start)});
  34.     long long int time_through_portal = time_to_olymp_from_portal + time_to_start_from_portal + 1;
  35.     long long int time_to_olymp_from_start = abs(start - olymp);
  36.     long long int &res = *min_element({time_through_portal, time_to_olymp_from_start});
  37.     cout<<res;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement