raghuvanshraj

Untitled

Apr 10th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. /**
  2.  *    author:   raghuvanshraj
  3.  *    created:  10.04.2020 11:39:49 PM
  4. **/
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7.  
  8. #define MOD 1000000007
  9.  
  10. #define SET_ARR(arr,n,val) for (int i = 0; i < n; ++i) arr[i] = val
  11. #define SET_ARR2D(arr,n,m,val) for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) arr[i][j] = val
  12. #define INPUT_ARR(arr,n) for (int i = 0; i < n; ++i) cin >> arr[i];
  13. #define INPUT_ARR2D(arr,n,m) for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) cin >> arr[i][j];
  14. #define PRINT_ARR(arr) for (auto x : arr) cout << x << ' '; cout << endl
  15. #define PRINT_ARR2D(arr) for (auto x : arr) { for (auto y : x) cout << y << ' '; cout << endl; }
  16.  
  17. #define ALL(v) v.begin(), v.end()
  18. #define RALL(v) v.rbegin(), v.rend()
  19.  
  20. using namespace std;
  21. using namespace __gnu_pbds;
  22.  
  23. typedef long long ll;
  24. typedef unsigned long long ull;
  25. typedef long double ld;
  26.  
  27. template <typename T>
  28. using indexed_set = tree<T, null_type,less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  29.  
  30. template <typename T>
  31. using max_heap = priority_queue<T>;
  32.  
  33. template <typename T>
  34. using min_heap = priority_queue<T, vector<T>, greater<T>>;
  35.  
  36. template <
  37.     typename T,
  38.     typename = typename enable_if<is_arithmetic<T>::value, T>::type,
  39.     typename U,
  40.     typename = typename enable_if<is_arithmetic<U>::value, U>::type
  41. >
  42. pair<T,U> operator+(const pair<T,U> &a, const pair<T,U> &b) {
  43.     return {a.first + b.first, a.second + b.second};
  44. }
  45.  
  46. template <
  47.     typename T,
  48.     typename = typename enable_if<is_arithmetic<T>::value, T>::type,
  49.     typename U,
  50.     typename = typename enable_if<is_arithmetic<U>::value, U>::type
  51. >
  52. pair<T,U> operator-(const pair<T,U> &a, const pair<T,U> &b) {
  53.     return {a.first - b.first, a.second - b.second};
  54. }
  55.  
  56. template <
  57.     typename T,
  58.     typename = typename enable_if<is_arithmetic<T>::value, T>::type,
  59.     typename U,
  60.     typename = typename enable_if<is_arithmetic<U>::value, U>::type,
  61.     typename V,
  62.     typename = typename enable_if<is_arithmetic<V>::value, V>::type
  63. >
  64. pair<T,U> operator*(const V &a, const pair<T,U> &b) {
  65.     return {a * b.first, a * b.second};
  66. }
  67.  
  68. int main(int argc, char const *argv[]) {
  69.     ios::sync_with_stdio(false);
  70.     cin.tie(0);
  71.    
  72.     int n,k;
  73.     cin >> n >> k;
  74.     vector<int> arr(n);
  75.     INPUT_ARR(arr,n);
  76.  
  77.     sort(ALL(arr));
  78.     k = min(k, n-k);
  79.     vector<int> mask(n);
  80.     int i,j;
  81.     if (k%2) {
  82.         i = j = n/2;
  83.     } else {
  84.         i = n/2 - 1;
  85.         j = n/2 + 1;
  86.     }
  87.  
  88.     while (k > 0) {
  89.         if (i == j) {
  90.             k--;
  91.         } else {
  92.             k -= 2;
  93.         }
  94.  
  95.         mask[i] = mask[j] = 1;
  96.         i -= 2;
  97.         j += 2;
  98.     }
  99.  
  100.     vector<int> set1, set2;
  101.     for (int i = 0; i < n; ++i) {
  102.         if (mask[i]) {
  103.             set1.push_back(arr[i]);
  104.         } else {
  105.             set2.push_back(arr[i]);
  106.         }
  107.     }
  108.  
  109.     ll ans = 0;
  110.     for (int x : set1) {
  111.         for (int y : set2) {
  112.             ans += (ll)abs(x-y);
  113.         }
  114.     }
  115.  
  116.     cout << ans;
  117.    
  118.     return 0;
  119. }
Add Comment
Please, Sign In to add comment