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

简介:


 

Problem

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.

The 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.

The 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

 


Problem setter: Josh Bao 

 

题目大意:n人围绕圆桌,每个人有一定数量金币,金币总数能被n整除。每个人可以给他左右的人一些金币,最终使每个人金币数相等。求被转手金币最小值。

 

解题思路:

  1、列出方程组:

    设Ai为初始金币数,xi表示i号给i-1号金币数[可正可负],M为最终每个人的金币数,则:

      for the first person:  A1-x1+x2=M-->x2=M-A1+x1=x1-C1[规定C1=A1-M]

       for the second person:   A2-x2+x3=M-->x3=M-A2+x2=2M-A1-A2+x1=x1-C2

       for the third person:   A3-x3+x4=M-->x4=M-A3+x3=3M-A1-A2-A3+x1=x1-C3

       ......

       for the nth person:  An-xn+x1=M[这是一个多余的式子,并不能给我们更多的信息]

  2、转换为单变量问题:

    我们希望所有xi的绝对值之和最小,即:|x1|+|x1-C1|+|x1-C2|+...+|x1-Cn|最小。

  3、转换为几何问题:

    注意上式的几何意义就是数轴上给定n个点找出一个到他们之和尽量小的点。

  4、递归求最优解:

    这个最优的x1就是他们的中位数。

 

 

复制代码
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 typedef long long LL;
 5 using namespace std;
 6 LL A[1000010],C[1000010];//A[i]表示第i人初始金币数
 7 int main(){
 8     int n;
 9     while(cin>>n){
10         LL tot=0;//总数
11         for(int i=0;i<n;i++){
12             cin>>A[i];
13             tot+=A[i];
14         }
15         LL M=tot/n;//平均数
16         C[0]=0;
17         for(int i=1;i<n;i++){
18             C[i]=C[i-1]+A[i]-M;//递推数组C
19         }
20         sort(C,C+n);
21         LL x1=C[n/2],ans=0;//计算x1
22         for(int i=0;i<n;i++){
23             ans+=abs(x1-C[i]);
24         }
25         cout<<ans<<'\n';
26     }return 0;
27 }
复制代码


相关文章
|
8天前
|
Java
三阶魔方公式详解及快速解法方法介绍
三阶魔方公式详解及快速解法方法介绍
|
2月前
【每日一题Day165】LC1039多边形三角剖分的最低得分 | 区间dp
【每日一题Day165】LC1039多边形三角剖分的最低得分 | 区间dp
32 0
|
2月前
考研高数之无穷级数题型一:判断收敛性、求收敛半径以及收敛域和收敛区间(题目讲解)
考研高数之无穷级数题型一:判断收敛性、求收敛半径以及收敛域和收敛区间(题目讲解)
203 0
|
11月前
|
人工智能 算法 C语言
LeetCode.每日一题 1039. 多边形三角剖分的最低得分
这题是一道区间Dp问题,将一个多边形形划分为若干个三角形,求其最小的得分.
70 0
|
人工智能 算法 C++
每日算法系列【LeetCode 1039】多边形三角剖分的最低得分
每日算法系列【LeetCode 1039】多边形三角剖分的最低得分
每日三题-最大正方形 、完全平方数 、目标和
每日三题-最大正方形 、完全平方数 、目标和
68 2
每日三题-最大正方形 、完全平方数 、目标和
|
机器学习/深度学习 算法 Java
[洛谷 P3381] 最小费用最大流 | 模板 Bellman-Ford寻找增广路 入门
题目描述 给出一个包含 n nn 个点和 m mm 条边的有向图(下面称其为网络) G = ( V , E ) G=(V,E)G=(V,E),该网络上所有点分别编号为 1 ∼ n 1 \sim n1∼n,所有边分别编号为 1 ∼ m 1\sim m1∼m,其中该网络的源点为 s ss,汇点为t tt,网络上的每条边 ( u , v ) (u,v)(u,v) 都有一个流量限制 w ( u , v ) w(u,v)w(u,v)和单位流量的费用 c ( u , v ) c(u,v)c(u,v)。
119 0
PTA7-76 求幂级数展开的部分和 (20分)
PTA7-76 求幂级数展开的部分和 (20分)
424 0
|
人工智能
【洛谷】【区间dp】【高精度】P1005 [NOIP2007 提高组] 矩阵取数游戏
【洛谷】【区间dp】【高精度】P1005 [NOIP2007 提高组] 矩阵取数游戏
271 0