MiinaMagdy

sum of Three values

Aug 16th, 2021 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. /*                                    
  2.            .@@@@@@@@   @@@                                              @@       @@
  3.        ,@@@@@@     @@ .@@@      @@    @@@        @@@%@@@@.   @@@        @@  @@  @@@@/      @@@
  4.        /& @@@@    @@@ *@@@     @@@#  @@@&@@@    @@@@@@@#&@@ @@@%&,      @@ @@@@  @@@@    @@@&
  5.           @@@  .@@@   /@%@     @@@  @@@@   @@& .@@@         @@&&&@      @@ @@@@  (@@@@ @@@@
  6.          %@@@@@@&     /&&&     %@&  @&@     @& @&@&         &&@#%@%.   &@@ &@&#   %@@@@@@
  7.          @@@@@        /@&%(%&@@@&@  @&&    ,@@ @@&&@@&@@@   &@@ /@@&/  @@, @@@/    %@@@(
  8.          @@@.         *@@@@#,  @@@  @@@   ,@@  @@@.         @@@   @@@@@@@  %@@/   @@@@@@%
  9.          @@@          *@@%     @@@   @@@@@@#    @@@@@@@@@@  @@@    ,@@@@    @@& .@@@  @@@@
  10.          &@@,          @@%      @@                          @@@                 @@      @@@
  11.                                                              &*
  12.  */
  13. #include <bits/stdc++.h>
  14. // #include "phoenix.h"
  15. #define ceil(n, m) (((n) / (m)) + ((n)%(m) ? 1 : 0))
  16. #define endl "\n"
  17. #define NumOfDig(n) log10(n) + 1
  18. #define MOD 1000000007
  19. #define INF 2000000000
  20. #define Time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs" << "\n";
  21. #define EPS 1e-9
  22. #define PI1 acos(-1)
  23. #define PI2 3.141592653
  24. #define all(s) s.begin(), s.end()
  25. #define rall(s) s.rbegin(), s.rend()
  26. #define getline(s) getline(cin >> ws, s)
  27.  
  28. using namespace std;
  29.  
  30. typedef long long ll;
  31. typedef unsigned long long ull;
  32.  
  33. /**
  34.  * @author MiinaMagdy 😌🙋‍♂️
  35.  * @remark Time limit - memory limit (efficiency)
  36.  * @remark (OVERFLOW) long long
  37.  * @remark (CORNER) test case
  38.  * @remark division by (ZERO) || Out of array's (RANGE)
  39.  * @remark use logarithm if you want to compare two products
  40.  */
  41.  
  42. void phoenix()
  43. {
  44.     ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  45.     #ifndef ONLINE_JUDGE
  46.         freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  47.     #endif
  48.     Time
  49. }
  50.  
  51. struct Number
  52. {
  53.     ll value, index;
  54. };
  55.  
  56. int main(void)
  57. {
  58.     phoenix();
  59.     int testcases = 1;
  60.     // cin >> testcases;
  61.     while (testcases--)
  62.     {
  63.         ll n, x;
  64.         cin >> n >> x;
  65.         vector<Number> nums(n);
  66.         for (int i = 0; i < n; i++)
  67.         {
  68.             cin >> nums[i].value;
  69.             nums[i].index = i + 1;
  70.         }
  71.         sort(nums.begin(), nums.end(), [](Number a, Number b) -> bool
  72.         {
  73.             if (a.value == b.value) return a.index < b.index;
  74.             return a.value < b.value;
  75.         });
  76.         // for (auto & i : nums) cout << i.value << " ";
  77.         for (ll i = 0; i < n - 2; i++)
  78.         {
  79.             ll sum = nums[i].value;
  80.             ll l = i + 1, r = n - 1;
  81.             while (l < r)
  82.             {
  83.                 if (sum + nums[l].value + nums[r].value == x) return cout << nums[i].index << " " << nums[l].index << " " << nums[r].index, 0;
  84.                 if (sum + nums[l].value + nums[r].value > x) r--;
  85.                 else if (nums[l].value + nums[r].value + sum < x) l++;
  86.             }
  87.         }
  88.         return cout << "IMPOSSIBLE", 0;
  89.     }
  90.     return 0;
  91. }
Add Comment
Please, Sign In to add comment