首页 > 其他分享 >Hay Bales

Hay Bales

时间:2024-03-31 17:05:33浏览次数:6  
标签:hay int Bales height Hay bales pile 草包

题目描述

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;
}

标签:hay,int,Bales,height,Hay,bales,pile,草包
From: https://blog.csdn.net/Lyh1gguyg/article/details/137203333

相关文章

  • Haystack
     1.什么是HaystackHaystack是django的开源全文搜索框架(全文检索不同于特定字段的模糊查询,使用全文检索的效率更高),该框架支持Solr,Elasticsearch,Whoosh,**Xapian搜索引擎它是一个可插拔的后端(很像Django的数据库层),所以几乎你所有写的代码都可以在不同搜索引擎之间便捷切换......
  • 猜数游戏[USACO2008] Haybale Guessing G
    $Haybale\Guessing\G$(猜数游戏)解题报告\(Diffculty:\)\(\color{purple}省选/NOI-\)传送门1:(HZOIER)传送门2:(vjudge)传送门3:(luogu)题面为了提高自己低得可怜的智商,奶牛们设计了一个新的猜数游戏,来锻炼她们的逻辑推理能力。游戏开始前,一头指定的奶牛会在牛棚后......
  • UVA10295 Hay Points 题解
    题目大意:给你\(n\)个单词,每一个单词的值为\(v_i\),让你求出在一个文章段落里的出现过的单词的值之和。思路:可以用STL库中的map来存储一个单词的值,最后在处理的时候可以直接累加。附上你们最期待的代码:#include<bits/stdc++.h>usingnamespacestd;map<string,int......
  • P2898 [USACO08JAN] Haybale Guessing G 题解
    题目传送门前置知识二分答案|并查集解法对条件的合法性判断其他题解已经讲得很明白了,这里不再赘述。这里主要讲一下用并查集实现黑白染色问题。以下内容称被覆盖为黑色,不被覆盖为白色。本题因为是单向染色,即从白到黑,故可类似luoguP1840ColortheAxis和D的并查集或......
  • 【法语阅读】Hayao Miyazaki commencera à travailler sur son prochain film lorsqu
    HayaoMiyazakicommenceraàtravaillersursonprochainfilmlorsqueLegarçonetlehéronneseraplusàl’affichedanslessallesdecinémaHayaoMiyazakiwillstartworkingonhisnextfilmwhen'TheBoyandtheHeron'isnolongershowingin......
  • 谈谈与Elasticsearch创始人Shay Banon面对面交流后的意外收获
    还记2017年11月,当时在阿里巴巴云栖大会上Elasticsearch与阿里云宣布达成战略合作,为中国市场提供崭新的用户体验。当时在现场听完Elasticsearch创始人ShayBanon的讲演受益匪浅,他在搜索的领域至今已经深耕了20多年,Elasticsearch第一个公开版本在2010年2月发布,2012年创建Elastic公司......
  • 题解 P2903 【[USACO08MAR]The Loathesome Hay Baler S】
    postedon2021-05-0320:50:49|under题解|source首先输入,记录一下哪个齿轮的位置在\((0,0)\),哪个在\((x_t,y_t)\)。接着,为了避免多次判断两个齿轮相切而超时,我们可以预处理一个\(link_{i,j}\),表示第\(i\)个齿轮是否和第\(j\)个齿轮相切。这部分直接\(O(n^2)\)暴......
  • [USACO08NOV]Buying Hay S
    [USACO08NOV]BuyingHayS题目描述FarmerJohnisrunningoutofsuppliesandneedstopurchaseH(1<=H<=50,000)poundsofhayforhiscows.HeknowsN(1<=N<=100)haysuppliersconvenientlynumbered1..N.Supplierisellspackagesthatcon......
  • HayStack
    HayStack一、什么是Haystack?Haystack是django的开源全文搜索框架(全文检索不同于特定字段的模糊查询,使用全文检索的效率更高),该框架支持Solr,Elasticsearch,Whoosh,**Xapian搜索引擎它是一个可插拔的后端(很像Django的数据库层),所以几乎你所有写的代码都可以在不同搜索引擎之间......
  • déce. 23 Out of Hay S
    https://www.luogu.com.cn/problem/P1547最小生成树中的最长边是最后被加入的#include<bits/stdc++.h>usingnamespacestd;#defineinRead()typedeflonglongll;......