Description

有n个小朋友坐成一圈,每人有ai个糖果。每人只能给左右两人传递糖果。每人每次传递一个糖果代价为1。

Input

第一行一个正整数nn<=1'000'000,表示小朋友的个数.
接下来n行,每行一个整数ai,表示第i个小朋友得到的糖果的颗数.

Output

求使所有人获得均等糖果的最小代价。

Sample Input

4
1
2
5
4

Sample Output

4
这道题让我想到了白书上面的相似的一道题:

UVA11300 Spreading the Wealth

A Communist regime is trying to redistribute wealth in a village. They have have decided to sit everyone around a circular table. First, everyone has converted all of their properties to coins of equal value, such that the total number of coins is divisible by the number of people in the village. Finally, each person gives a number of coins to the person on his right and a number coins to the person on his left, such that in the end, everyone has the same number of coins. Given the number of coins of each person, compute the minimum number of coins that must be transferred using this method so that everyone has the same number of coins.

Input

There is a number of inputs. Each input begins with n (n < 1000001), the number of people in the village. n lines follow, giving the number of coins of each person in the village, in counterclockwise order around the table. The total number of coins will fit inside an unsigned 64 bit integer.

Output

For each input, output the minimum number of coins that must be transferred on a single line.

Sample Input

3
100
100
100
4
1
2
5
4

Sample Output

0
4
题意:n个人围成一圈,每个人都有一些硬币,,每个人只能给左右相邻的人硬币,问最少交换几个硬币,使每个人硬币一样多。
我第一反应:费用流。第二反应:费用流。第三反应:费用流。
然后看uva11300题解:
首先,最终每个人金币数量可以计算出来,我们用M表示每个人最后拥有的金币数。
我们对于第i个人可以看作他给了他左边的人xi个金币(负数表示反向传递),他右边的人给了他xi+1个金币,假设他一开始拥有的金币数量为Ai,那么有Ai-xi+xi+1=M。
如果我们继续列下去就会变成这样:
A1-x1+x2=M -> x2=M-A1+x1 ->  令C1=A1-M -> x2=x1-C1
A2-x2+x3=M -> x3=M-A2+x2=2*M-A1-A2+x1 ->  令C2=A1+A2-2*M -> x3=x1-C2
A3-x3+x4=M -> x4=M-A3+x3=3*M-A1-A2-A3+x1 ->  令C3=A1+A2+A3-3*M -> x4=x1-C3
.......
那么我们就会得到n个等式,但是我们发现我们可以用这n个等式中的任意n-1个去变换得到最后剩下那个等式。
因为我们知道 $\sum A_i =M \times n $ ,那么最后一个等式Cn=0,就是x1=x1
也就是说,最后那个等式是没有用的,我们只会用到n-1个等式。那么这n-1个等式可以用来做什么呢,我们怎样才能求得 $ \sum abs(x_i) $ 最小值呢?
$ \sum abs( x_i ) = abs( x_1 )+abs( x_2 )+abs( x_3 )+......+abs( x_n )=abs( x_1 ) +abs(x_1-C_1) +abs(x_1- C_2)+ ......+abs(x_1-C_{n-1})$

由于Ci是可以直接计算出来的,可以当作常数,所以说这个等式相当于是求一个最优的x1,使得数轴上x1到Ci距离和最小,而这个距离和就是我们所求的答案。
最后问题转化成求数轴上一个点到所有已知的点最小距离,相信在小学(或是初中)数学老师都讲过,这样的点就是中位数啦。
然后就直接算出C,然后算中位数与其他的所有的点的距离。
bzoj1045 代码:
//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=1e6+10;
long long n,tot,ans,c,A[maxn],C[maxn];long long aa;char cc;
long long read() {aa=0;cc=getchar();while(cc<'0'||cc>'9') cc=getchar();while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();return aa;
}int main() {n=read();for(int i=1;i<=n;++i) A[i]=read(),tot+=A[i];tot/=n;for(int i=1;i<n;++i) C[i]=A[i]-tot+C[i-1];sort(C+1,C+n+1);//还有一个C[i]为0的 for(int i=1;i<=n;++i) ans+=abs(C[i]-C[(n+1)>>1]);printf("%lld",ans);return 0;
}

  

转载于:https://www.cnblogs.com/Serene-shixinyi/p/7594077.html

bzoj1045 糖果传递相关推荐

  1. BZOJ-1045 糖果传递 数学+递推

    1045: [HAOI2008] 糖果传递 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2975 Solved: 1327 [Submit][Sta ...

  2. BZOJ-1045 糖果传递

    先拆成链的情况来看. 设B[i]表示i要向i+1拿糖果的数量,C为平均数,则B[i] = C - A[i] + B[i-1] Answer就是B的绝对值之和 现在来看环的情况,也就是说B[n]指的是n ...

  3. 【BZOJ1045】【codevs1868】糖果传递,数学贪心

    糖果传递 2008年 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Description 有n个小朋友坐成一圈,每人有ai个糖果 ...

  4. 糖果传递 (数学题)

    糖果传递                                                                                                 ...

  5. bz10451045: [HAOI2008] 糖果传递

    1045: [HAOI2008] 糖果传递 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 2958  Solved: 1319 [Submit][S ...

  6. bzoj 1045: [HAOI2008]糖果传递

    1045: [HAOI2008] 糖果传递 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4094  Solved: 1970 [Submit][S ...

  7. 中位数应用-货仓选址-纸牌均分-糖果传递-七夕祭

    1. 货仓选址 描述 在一条数轴上有N家商店,它们的坐标分别为 A[1]~A[N].现在需要在数轴上建立一家货仓,每天清晨,从货仓到每家商店都要运送一车商品.为了提高效率,求把货仓建在何处,可以使得货 ...

  8. AcWing 122. 糖果传递【贪心】【《算法竞赛进阶指南》,微软面试题 , HAOI2008】

    AcWing 122. 糖果传递 一.题目链接 二.题目分析 (一)算法标签 (二)解题思路 三.AC代码 四.其它题解 一.题目链接 AcWing 122. 糖果传递 进阶题目 AcWing 105 ...

  9. 洛谷T68695 mxj的新年礼物(糖果传递)

    题目背景 在新的一年,mxj给俱乐部的孩子们准备了一堆的礼物,它生成了一个随机数列表a,使得第i个人会得到a[i]件礼物. 但是,因为某些人得到的礼物太多,为了公平起见,mxj建议大家把礼物平均分配. ...

最新文章

  1. 物联网时代营销怎么做?
  2. Python基础-socket编程
  3. 计算机网络多媒体图像矢量图,13多媒体信息处理——图像处理(一)
  4. 优化 ASP.NET Core Docker 镜像的大小
  5. 通达信手机版指标源码大全_通达信指标公式源码短炒买卖指标
  6. linux 分割pdf,PDFBox分割PDF文档
  7. 数据分析 数据科学_数据科学中的数据分析
  8. AttributeError: ‘SMOTE’ object has no attribute ‘fit_sample’
  9. 伟创力回应扣押华为物资;谷歌更新图片界面;Python 3.8.0b3 发布 | 极客头条
  10. win11如何显示所有应用图标 Windows11显示所有应用图标的设置方法
  11. Python 房贷计算器小工具
  12. 极速PDF打开文件后工具栏不显示怎么办
  13. 解决Ubuntu18.04搜狗输入法无法使用的问题
  14. 软件开发中如何评估工作量
  15. android es2 es3,Android模拟器和OpenGL ES3:EGL_BAD_CONFIG
  16. png转ico图标的方法
  17. 六一儿童节 python
  18. 学专业计算机的配置,大学学计算机专业的学生电脑普遍配置是什么呢?
  19. GPS定位系统(二)——Android端
  20. windows录屏_电脑录屏软件哪个好用?试试这个专业方法

热门文章

  1. 事物与数据库底层数据
  2. jvm堆空间的常用参数设置
  3. Mac查看本机ip地址
  4. 后端技术:这35 个细节,提升你的 Java 代码质量
  5. 好RESTful API的设计原则
  6. servlet html登录,Servlet实现用户登录
  7. java scanner接收数组_java – 使用scanner将文件中的整数读入数组
  8. MySql 创建存储过程
  9. Promise 到底是什么?看这个小故事
  10. 武汉区块链软件公司:区块链游戏和普通的游戏有什么区别?