# HankerRank Equal(贪心)

Christy is interning at HackerRank. One day she has to distribute some chocolates to her colleagues. She is biased towards her friends and plans to give them more than the others. One of the program managers hears of this and tells her to make sure everyone gets the same number.

To make things difficult, she must equalize the number of chocolates in a series of operations. For each operation, she can give  chocolates to all but one colleague. Everyone who gets chocolate in a round receives the same number of pieces.

For example, assume the starting distribution is . She can give  bars to the first two and the distribution will be . On the next round, she gives the same two  bar each, and everyone has the same number: .

Given a starting distribution, calculate the minimum number of operations needed so that every colleague has the same number of chocolates.

Input Format

The first line contains an integer , the number of testcases.

Each testcase has  lines.
- The first line contains an integer , the number of colleagues.
- The second line contains n space-separated integers denoting the number of chocolates each colleague has.

Constraints

Number of initial chocolates each colleague has <

Output Format

Print the minimum number of operations needed for each test case, one to a line.

Sample Input

1
4
2 2 3 7


Sample Output

2


Explanation

Add  to all but the 3rd element
Add  to all but the 4th element

Two operations were required.

Sample Input 1

1
3
10 7 12


Sample Output 1

2


Explanation 1

Add  to the first two elements
Add  to the last two elements

Two operations were required.

/*by kzl*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>

using namespace std;
const int maxx = 1e5+500;
const int INF = 0x3f3f3f3f;
typedef long long LL;

int equal1(vector <int> arr) {
int n = arr.size();
int mi = 1e9;
int ans = 0;
for(int i=0;i<n;i++){
int a = arr[i];
ans+=arr[i];
mi = min(mi,a);
}
int sum = 0;
int ma = 1e9;
for(int k=mi;k>=mi-5;k--){
sum = 0;
for(int i=0;i<n;i++){
int aa = arr[i] - k;
sum+=aa/5;
aa = aa%5;
sum+=aa/2;
aa = aa%2;
sum+=aa;
}
ma = min(ma,sum);
}
return ma;
}

int main() {
int t;
cin >> t;
for(int a0 = 0; a0 < t; a0++){
int n;
cin >> n;
vector<int> arr(n);
for(int arr_i = 0; arr_i < n; arr_i++){
cin >> arr[arr_i];
}
int result = equal1(arr);
cout << result << endl;
}
return 0;
}


PS：虽然网站把题目归类为DP，但我不知道怎么用动规解QAQ

• 本文已收录于以下专栏：

举报原因： 您举报文章：HankerRank Equal(贪心) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)