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

Start with  
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

Start with  
Add  to the first two elements  
Add  to the last two elements 

Two operations were required.

注意:不知道现在题面是否已经更改,不过实际上他可以增加的数是1,2,5而非1,3,5.

题目链接:https://www.hackerrank.com/challenges/equal/problem

思路:因为每一回合可以给n-1个人同时+{1,2,5}任选一个值。要求最后所有人数值相同。那么可以反向思考,即每一回合可以给某一个人-{1,2,5},也是求最少次数使得最后数值相等。(因为无论是加n-1个人还是减一个人,他们两种操作造成的的差值是相同的),那么就可以贪心的选择当前最大的开始减,优先-5,在减2,最后减一。

然后关于最终所有值都相同,但是最终这个值一定是所有数中的最小值吗?很有诱惑性,给一组样例,好好体会==!    (2,6,6,6,6),这说明最终这个值不一定是数组中的最小值,但必定是(min-5,min】中的一个值。上面例子2-1,6-5,6-5,6-5,6-5得到1,1,1,1,1总共五步操作。

代码如下:

/*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 贪心
个人分类: 贪心
上一篇LeetCode 786. K-th Smallest Prime Fraction(优先队列)
下一篇HackerRank Sherlock and Cost (动态规划)
想对作者说点什么? 我来说一句

贪心算法经典例题.doc

2010年12月25日 35KB 下载

没有更多推荐了,返回首页

关闭
关闭