题目大意:n个人手中有些金币,每个人可给相邻两个人一些金币,使得最终每个人手中金币数相同,求被转手的金币最少数

m为最终每个人手中的金币数,a1,a2,a3,...,an为每个人开始时手中的金币数,x1,x2,x3,...,xn为每个人转手的金币数
  • 假设 4个人 号分别为 1 2 3 4
x2  :为2号给1号的金币数 
x3  :为3号给2号的金币数
x4  :为4号给3号的金币数
x1  :为1号给4号的金币数
设:c1 = a1 - m(即:cn = an - m);
C1 = c1 = a1 - m;
C2 = c1 + c2 = C1 + c2 = C1 + a2 - m;
C3 = c1 + c2 + c3 = C2 + c3 = C2 + a3 - m;
...
Cn = C(n - 1) + an - m;
1号 :m = a1 + x2 - x1;  x2 = m + x1 - a1 = x1 - c1 = x1 - C1;(1号最终所得金币为其原有的金币加上2号所给的减去其给4号的金币所得,下面同理)
2号 :m = a2 + x3 - x2;  x3 = m + x2 - a2 = x2 - c2 = x1 - c1 - c2 = x1 - C2;
3号 :m = a3 + x4 - x3;  x4 = m + x3 - a3 = x3 - c3 = x1 - c1 - c2 - c3 = x1 - C3;
...
xn = x1 - C(n - 1);
求转手金币的最小值(即求x1 + x2 + x3 +...+xn的最小值即求x1+|x1-C1|+|x1-C2|+|x1-C3|+...+|x1-C(n-1)|的最小值)
其几何意义为求一个点到n个点距离和的最小值(即数轴上点x1到Ci的距离和的最小值)
若想使其距离和最小,则x1需满足的条件是:点x1必须为n个点中的中位数
若点数n为奇数则点x1必须与中间那个点重合(即中位数)
若点数n为偶数则点x1必须在中间两个点之间(仍是中位数) #include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define N 1000010 long long a[N], c[N], sum, ans, m, x1;
int cmp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
} int main()
{
int n, i;
while(scanf("%d", &n) != EOF)
{
sum = ans = ;
for(i = ; i < n ; i++)
{
scanf("%lld", &a[i]);
sum += a[i];
}
m = sum / n;
c[] = ;
for(i = ; i < n ; i++)
c[i] = c[i - ] + a[i] - m;
qsort(c, n, sizeof(c[]), cmp);
x1 = c[n / ];
for(i = ; i < n ; i++)
ans += fabs(x1 - c[i]);
printf("%lld\n", ans);
}
return ;
}

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(数学题)

    UVA - 11300 Spreading the Wealth [题目描述] 圆桌旁边坐着n个人,每个人有一定数量的金币,金币的总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金 ...

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

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

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

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

  8. Math - Uva 11300 Spreading the Wealth

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

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

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

  10. UVa 11300 Spreading the Wealth 分金币

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

随机推荐

  1. python写的第一个简单小游戏-猜数字

    #Filename:game1.py guess=10 running=True while running: try: answer=int(raw_input('Guess what i thin ...

  2. 【转】android UI设计的一些心得与问题解决(无效果图)

    1.把Button或者ImageButton的背景设为透明或者半透明: 半透明<Buttonandroid:background="#e0000000" ... /> ...

  3. JAVA操作数据库插入中文表中显示乱码的解决方法

    String dbUrl = "jdbc:mysql://localhost:3306/BookDB?useUnicode=true&characterEncoding=GB2312 ...

  4. 4197: [Noi2015]寿司晚宴

    状压dp. 500分解质因数的话,除了最大的质因数只需要8个质数,用二进制x储存,最大的质因数用y来储存(若没有比那8个质数大的质因数就使y=1) 用f[i][j]表示第一个人方案为i,第二个人方案为 ...

  5. Asp.net正则获取html内容

    1.获取div内容 string str = "tt<u>ss</u><div id=\"test\"><div>< ...

  6. 转载:iOS 推送的服务端实现

    参考网址1: iOS消息推送机制的实现 http://www.cnblogs.com/qq78292959/archive/2012/07/16/2593651.html 参考网址2: iOS 推送的 ...

  7. codevs 1088 神经网络

    bfs.语文题. #include<iostream> #include<cstdio> #include<cstring> #include<algorit ...

  8. BZOJ 3527 力

    fft推下公式.注意两点: (1)数组从0开始以避免出错. (2)i*i爆long long #include<iostream> #include<cstdio> #incl ...

  9. TCP/IP详解学习笔记(10)-TCP连接的建立与中止

    TCP是一个面向连接的协议,所以在连接双方发送数据之前,都需要首先建立一条连接.这和前面讲到的协议完全不同.前面讲的所有协议都只是发送数据而已,大多数都不关心发送的数据是不是送到,UDP尤其明显,从编 ...

  10. Android-监听sdcard状态

    public class MyService extends Service { private static final String TAG = "MyService"; Fi ...