HankerRank Equal(贪心)

原创 2018年04月17日 18:24:41

题目如下:

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



编程修养(三)

6、if 语句对出错的处理———————————我看见你说了,这有什么好说的。还是先看一段程序代码吧。    if ( ch >= 0 && ch         /* 正常处理代码 */    }e...
  • haoel
  • haoel
  • 2003-05-08 12:12:00
  • 17701

HankerRank Equal(贪心)

题目如下: Christy is interning at HackerRank. One day she has to distribute some chocolates t...
  • lwgkzl
  • lwgkzl
  • 2018-04-17 18:24:41
  • 14

Hankerrank World CodeSprint #4 Gridland Provinces (字符串双hash )

Observe that since the knight always completes his journey in minimum total time, he never visits th...
  • kalilili
  • kalilili
  • 2016-06-27 18:01:16
  • 502

uva 10716 Evil Straw Warts Live(贪心回文串)

这道题目我用了一上午才做出来,还是看的别人的思路,虽然没有看代码做的有点慢,代码能力还是得加强啊,思维 得缜密,不能想当然,要有依据,写上的代码要有准确度,省的以后还得慢慢调试 思路:贪心,每次都...
  • sinat_22659021
  • sinat_22659021
  • 2015-08-07 11:42:38
  • 498

HackerRank网站,为编码程序员们提供一个以编码谜题和现实生活中遇到的编码难题为基础的新兴的社交平台

https://www.hackerrank.com/        HackerRank网站,为编码程序员们提供一个以编码谜题和现实生活中遇到的编码难题为基础的新兴的社交平台。        Ha...
  • xindoo
  • xindoo
  • 2013-01-09 16:40:11
  • 5802

Equal (贪心,思维)

题目大意给你一个数组,每次操作是固定一个数,其他的数可以+1、+2、+5.问至少经过几次操作可以让整个数组的数字都一样?题目分析:其实这道题最难的点在问题的逆向转化:原操作变成全相等的次数等价于每次只...
  • cmershen
  • cmershen
  • 2017-01-03 17:16:36
  • 249

贪心算法经典例题.doc

  • 2010年12月25日 13:32
  • 35KB
  • 下载

C# 中==和Equal的区别

http://new-fighter.iteye.com/blog/1634800 今天突然看到一种情况,颠覆了我对这比较使用方法的判断。  于是开始在网上找资料,但几乎都是Java的,好...
  • OnafioO
  • OnafioO
  • 2016-02-01 13:56:06
  • 1076

String中==和equal()的区别

==是判断两个字符窜引用是否相等。 .equal()判断的是两个字符窜值是否相等。 不同对象总是产生不同的散列值,即便是他们是相等的值也可以。 因此,如果你想要判断两个字符串是否相等你最好使用.equ...
  • l975764577
  • l975764577
  • 2014-03-09 19:18:32
  • 1575

java 中“==”与“equal” 的区别

一、java当中的数据类型和“==”的含义:   基本数据类型(也称原始数据类型) :byte,short,char,int,long,float,double,boolean。他们之间的比较...
  • qq_33406883
  • qq_33406883
  • 2016-08-11 10:29:19
  • 8986
收藏助手
不良信息举报
您举报文章:HankerRank Equal(贪心)
举报原因:
原因补充:

(最多只允许输入30个字)