UVA - 11300 Spreading the Wealth

【题目描述】

圆桌旁边坐着n个人,每个人有一定数量的金币,金币的总数能被n整除。每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数量相等。您的任务是求出被转手的金币的数量的最小值。

【输入格式】

输入包含多组数据。每组数据第一行为一个整数n(n<=1000000)0),以下n行每行为一个整数,按逆时针顺序给出每个人拥有的金币数。输入结束标志为文件结束符(EOF)

【输出格式】

对于每组数据,输出被转手的金币的数量的最小值。输入保证这个值在 64位无符号整数的范围之内。

【Sample】

Input

3
100
100
100
4
1
2
5
4

Output

0
4

【Solution】

这是一道数学题

根据题意

每个人都可以给两边人传递硬币

为了简化问题

我们定向每个人i只能给下一个人x[i]枚硬币

那么我们的答案就是求\(|x_1| + |x_2| + ... + |x_n|\)的最小值

假设最终每个人分到num枚硬币

那么当前第i个人的情况是

a[i] - x[i] + x[i + 1] = num;

接下来就是推导式子

\(x_{i+1} = num - a_i + x_i\)

\(x_2 = num - a_1 + x_1\)

\(x_3 = num - a_2 + x_2\)

联立上述两个式子得到

\(x_2 = x_1 - (a_1 - num)\);

\(x_3 = x_2 - (a_1 + a_2 - 2 * num)\)

同理我们可以写出\(1\) ~ \(n\)所有的项数

所有的式子相加可以得到

$x_i = x_1 - $ \(\sum_{i = 1}^{n - 1} a_j - (n - 1) * m\)

由此可见,我们要求得答案只与\(x_1\)有关

拿一个\(tmp_i\)表示后面的一大坨\(\sum_{i = 1}^{n - 1} a_j - (n - 1) * m\)

我们的答案的表达式为

\(|x_1| + |x_2| + ... + |x_n| = |x_1| + |x_1 - tmp_2| + |x_1- tmp_3| + ... + |x_1- tmp_n|\)

根据这是一道数学题

不难想到

上述等式右边表示\(x _ 1\)到\(tmp_1\)~\(tmp_n\)的距离之和

显然,当\(x_1\)为集合$ {tmp_n} $的中位数时

该式子取得最小值

代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define int long long
using namespace std; inline int read(){
int x = 0, w = 1;
char ch = getchar();
for(; ch > '9' || ch < '0'; ch = getchar()) if(ch == '-') w = -1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return x * w;
} const int maxn = 1000010;
int a[maxn], tmp[maxn];
int num, n; signed main(){
while(scanf("%lld", &n) != EOF){
memset(a, 0, sizeof a);
memset(tmp, 0, sizeof tmp);
int ans = 0;
int sum = 0;
for(int i = 1; i <= n ;i++){
a[i] = read();
sum += a[i];
}
num = sum / n;
tmp[1] = 0;
for(int i = 1; i < n; i++){
tmp[i + 1] = num - a[i] + tmp[i];
}
sort(tmp + 1, tmp + 1 + n);
int x = tmp[n / 2];
for(int i = 1; i <= n; i++){
ans += abs(x - tmp[i]);
}
cout << ans << endl;
}
return 0;
}

UVA - 11300 Spreading the Wealth(数学题)的更多相关文章

  1. UVa 11300 Spreading the Wealth(有钱同使)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...

  2. uva 11300 - Spreading the Wealth(数论)

    题目链接:uva 11300 - Spreading the Wealth 题目大意:有n个人坐在圆桌旁,每个人有一定的金币,金币的总数可以被n整除,现在每个人可以给左右的人一些金币,使得每个人手上的 ...

  3. UVA.11300 Spreading the Wealth (思维题 中位数模型)

    UVA.11300 Spreading the Wealth (思维题) 题意分析 现给出n个人,每个人手中有a[i]个数的金币,每个人能给其左右相邻的人金币,现在要求你安排传递金币的方案,使得每个人 ...

  4. 数学/思维 UVA 11300 Spreading the Wealth

    题目传送门 /* 假设x1为1号给n号的金币数(逆时针),下面类似 a[1] - x1 + x2 = m(平均数) 得x2 = x1 + m - a[1] = x1 - c1; //规定c1 = a[ ...

  5. Uva 11300 Spreading the Wealth(递推,中位数)

    Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...

  6. Math - Uva 11300 Spreading the Wealth

    Spreading the Wealth Problem's Link ---------------------------------------------------------------- ...

  7. UVA 11300 Spreading the Wealth (数学推导 中位数)

    Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...

  8. [ACM_几何] UVA 11300 Spreading the Wealth [分金币 左右给 最终相等 方程组 中位数]

    Problem A Communist regime is trying to redistribute wealth in a village. They have have decided to ...

  9. UVa 11300 Spreading the Wealth 分金币

    圆桌旁坐着 n 个人,每个人都有一定数量的金币,金币总数能够被 n 整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等.你的任务是求出被转手的金币数量的最小值,比如 n = 4, ...

随机推荐

  1. spring Cloud服务注册中心Eureka集群

    spring Cloud服务注册中心Eureka集群配置: 在application.yml文件加以下配置: server: port: 8761 tomcat: uri-encoding: UTF- ...

  2. JNI_day01

    C语言简介 system()是提供C语言调用系统命令的函数 C语言基本数据类型 C语言中的整数类型:char/short/int/long C中使用ASCII保存字符,所以char所占用的字节数为1 ...

  3. 国外程序员整理的Java资源大全(全部是干货)

    原文 译者 唐尤华 翻译自 github akullpp 这里搜集了用来构建应用程序的工具. Apache Maven:Maven使用声明进行构建并进行依赖管理,偏向于使用约定而不是配置进行构建.Ma ...

  4. 20184302 2019-2020-2 《Python程序设计》实验四报告

    20184302 2019-2020-2 <Python程序设计>实验四报告 课程:<Python程序设计> 班级: 1843 姓名: 李新锐 学号:184302 实验教师:王 ...

  5. [computer graphics]简单光照模型(Phong和Blinn-Phong)和明暗处理

    简单光照模型(Phong和Blinn-Phong)和明暗处理 支持点光源和平行光,是一种简单光照模型,它将光照分解成了三个部分,分别为 漫反射 镜面反射 环境光 如图所示,是一个简单的几何模型. \( ...

  6. 安装并配置Samba

    1. 安装 samba ~$sudo apt-get install samba 2. 修改 samba 的配置文件 ~$sudo gedit /etc/samba/smb.conf 添加如下内容 [ ...

  7. TensorFlow从0到1之TensorFlow实现多元线性回归(16)

    在 TensorFlow 实现简单线性回归的基础上,可通过在权重和占位符的声明中稍作修改来对相同的数据进行多元线性回归. 在多元线性回归的情况下,由于每个特征具有不同的值范围,归一化变得至关重要.这里 ...

  8. CentOS7.5搭建spark2.3.1集群

    一 下载安装包 1 官方下载 官方下载地址:http://spark.apache.org/downloads.html 2  安装前提 Java8         安装成功 zookeeper  安 ...

  9. Docker镜像与容器的常用操作

    Docker镜像加速配置:Docker镜像常用操作:Dcoker容器常用操作. 镜像加速器 国内从 Docker Hub 拉取镜像有时会遇到困难,此时可以配置镜像加速器.国内很多云服务商都提供了国内加 ...

  10. 使用matlab进行图像处理的一些常用操作和tip

    本人还是习惯使用Python语言,有时候不得不使用matlab的时候就变得举步维艰,下面记录一下使用matlab进行图像处理的一些常用操作以及代码,方便之后查阅: 1. 图像的读取 %% 读取原图像 ...