SHARE
TWEET

Untitled

a guest Aug 20th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <vector>
  2. #include <climits>
  3. #include <iostream>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. int main(void) {
  9.     ios_base::sync_with_stdio(false);
  10.     cin.tie(nullptr);
  11.  
  12.     int tc;
  13.     cin >> tc;
  14.     for (int test_case = 1; test_case <= tc; test_case++) {
  15.         int n;
  16.         cin >> n;
  17.         vector<int> num(n);
  18.         vector<char> opvector;
  19.        
  20.         for (int i = 0; i < 4; i++) {
  21.             int x; cin >> x;
  22.             for (int j = 0; j < x; j++) {
  23.                 if (i == 0) opvector.push_back('+');
  24.                 else if (i == 1) opvector.push_back('-');
  25.                 else if (i == 2) opvector.push_back('*');
  26.                 else if (i == 3) opvector.push_back('/');
  27.             }
  28.         }
  29.         for (int i = 0; i < n; i++) {
  30.             cin >> num[i];
  31.         }
  32.         sort(opvector.begin(), opvector.end());
  33.         int minval, maxval;
  34.         minval = INT_MAX, maxval = INT_MIN;
  35.  
  36.         do {
  37.             int start = num[0];
  38.             for (int i = 0; i < opvector.size(); i++) {
  39.                 if (opvector[i] == '+') start += num[i + 1];
  40.                 else if (opvector[i] == '-') start -= num[i + 1];
  41.                 else if (opvector[i] == '*') start *= num[i + 1];
  42.                 else if (opvector[i] == '/') start /= num[i + 1];
  43.             }
  44.  
  45.             minval = min(minval, start);
  46.             maxval = max(maxval, start);
  47.         } while (next_permutation(opvector.begin(), opvector.end()));
  48.    
  49.         cout << "#" << test_case << " " << maxval - minval << '\n';
  50.     }
  51.  
  52.     return 0;
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top