raghuvanshraj

Untitled

Sep 4th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. /**
  2.  *    author:   raghuvanshraj
  3.  *    created:  04.09.2019 05:05:52 PM
  4. **/
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. int greedy_coin_change(int elt) {
  10.     int count = elt / 5;
  11.     elt %= 5;
  12.     count += elt / 2;
  13.     elt %= 2;
  14.     count += elt;
  15.  
  16.     return count;
  17. }
  18.  
  19. int min_rounds(int arr[], int n) {
  20.     int min_elt = *min_element(arr, arr + n);
  21.     int ans = INT_MAX;
  22.     for (int i = min_elt-4; i < min_elt+1; ++i) {
  23.         int curr_ans = 0;
  24.         for (int j = 0; j < n; ++j) {
  25.             curr_ans += greedy_coin_change(arr[j] - i);
  26.         }
  27.  
  28.         ans = min(ans, curr_ans);
  29.     }
  30.  
  31.     return ans;
  32. }
  33.  
  34. int main(int argc, char const *argv[]) {
  35.     ios::sync_with_stdio(false);
  36.     cin.tie(0);
  37.    
  38.     int t;
  39.     cin >> t;
  40.     while (t--) {
  41.         int n;
  42.         cin >> n;
  43.         int arr[n];
  44.         for (int i = 0; i < n; ++i) {
  45.             cin >> arr[i];
  46.         }
  47.  
  48.         cout << min_rounds(arr, n) << endl;
  49.     }
  50.    
  51.     return 0;
  52. }
Add Comment
Please, Sign In to add comment