Hay Bales

题目描述

The cows are at it again! Farmer John has carefully arranged N (1 <= N <=10,000) piles of hay bales, each of the same height. When he isn't looking, however, the cows move some of the hay bales between piles, so their heights are no longer necessarily the same. Given the new heights of all the piles, please help Farmer John determine the minimum number of hay bales he needs to move in order to restore all the piles to their original,equal heights. PROBLEM NAME: haybales INPUT FORMAT: * Line 1: The number of piles, N (1 <= N <= 10,000). * Lines 2..1+N: Each line contains the number of hay bales in a single pile (an integer in the range 1...10,000). SAMPLE INPUT (file haybales.in): 4 2 10 7 1 INPUT DETAILS: There are 4 piles, of heights 2, 10, 7, and 1. OUTPUT FORMAT: * Line 1: An integer giving the minimum number of hay bales that need to be moved to restore the piles to having equal heights. SAMPLE OUTPUT (file haybales.out): 7 OUTPUT DETAILS:

By moving 7 hay bales (3 from pile 2 to pile 1, 2 from pile 2 to pile 4, 2 from pile 3 to pile 4), we can make all piles have height 5.

翻译:

奶牛又来了!农夫约翰精心安排了 N (1 <= N <=10,000)一堆干草包,每个干草包的高度相同。然而,当他不在的时候看起来,奶牛在堆之间移动一些干草包,所以它们的高度不再相同。鉴于新的高度所有的干草堆,请帮助农夫约翰确定最少的干草数量他需要移动捆包才能将所有桩恢复到原来的样子,高度相等。

问题名称:干草包

输入格式: * 第 1 行:桩数,N (1 <= N <= 10,000)。

* 行 2..1+N:每行包含单个干草包的数量 pile(范围为 1...10,000 的整数)。

示例输入(文件 haybales.in): 4 2 10 7 1

输入详细信息: 有 4 个桩,高度为 2、10、7 和 1。

输出格式: * 第 1 行:一个整数,给出需要的最小干草包数量移动以将桩恢复到具有相等高度。 示例输出(文件 haybales.out): 7

输出详细信息: 通过移动 7 个干草包(3 个从 2 号桩移动到 1 号堆,2 个从 2 号堆移动到 4 号堆,2 个从桩 3 到桩 4),我们可以使所有桩的高度为 5。

样例输入 复制
4
2
10
7
1
样例输出 复制
7
提示

Solution Notes:

 We can calculate K by taking the total number of hay bales and dividing by N. Now that we know the target height K of each pile, let X be the total number of hay bales sitting at height above K. Each one of these hay bales must be moved at some point, so we know the optimal solution has to be at least as large as X. Moreover, we can always get by with moving at most X haybales by repeatedly moving a bale from any pile taller than K to any pile shorter than K until every pile has height K. Therefore, the answer is exactly X. It is important to note with this problem that we don't need to "explicitly" compute how the hay bales are supposed to be re-distributed in order to solve the problem.

解决方案说明:

我们可以通过取总数来计算 K 干草捆的数量并除以 N。现在我们知道了目标 每堆的高度 K,设 X 为坐置的干草包总数 在 K 以上的高度。这些干草包中的每一个都必须在一些地方移动 点,所以我们知道最优解必须至少与 X. 此外,我们总是可以通过最多移动 X 干草包来度过难关 反复将草捆从任何高于 K 的桩移动到任何桩 短于 K,直到每根桩的高度为 K。因此,答案 正好是 X。重要的是要注意这个问题,我们没有 需要“显式”计算干草包应该是什么样子 为了解决问题而重新分发。

代码

#include <iostream>
using namespace std;
const int N = 10010;
int a[N];
int main()
{
    int n,sum=0,ret=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        sum+=a[i];
    }
    int avg=sum/n;
    for(int i=0;i<n;i++)
    {
        int t=max(a[i]-avg,0);
        ret+=t;
    }
    printf("%d",ret);
    return 0;
}

  • 35
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值